How to store variables in PHP for use in different scripts?

I am really new to PHP programming, so the problem I am facing can be very simple, but I cannot find a solution on the Internet, and I tried several different methods, so any help let me appreciate it. The problem I am facing is that when a user logs in, I need their username to be stored in a variable that can be used when calling another PHP script. For example, when a user first logs in, the user name they entered is retrieved using a code similar to the one shown below:

    $username = ($_POST['entered_username']);
    mysql_real_escape_string($username);

Where 'entered username' is the input name of the user name in the form of a log in HTML code. The problem I am facing is how to save the $ username variable so that it can be used in another script. For example, when a user goes to a page where he / she can see his personal information, which is stored in a MySQL database. Ideally, I would like to use an SQL query, like the following below:

    $qry = ("select Username, Password, UserType from $table where Username = '".$username."'");

However, this is not possible since the $ username variable will not be defined in this script. So, how can I save the $ username variable in order to access it through another PHP script. Thank you in advance for your help and apologies if any of the information I provided is too vague.

+3
source share
3

$_SESSION. .

session_start();
$username = ($_POST['entered_username']);
mysql_real_escape_string($username);
$_SESSION['username'] = $username;

, :

session_start();
$qry = ("select Username, Password, UserType from $table where Username = '".$_SESSION['username']."'");

, session_start() , HTML- . .

0

: SESSION.

, :

session_start();

:

$_SESSION['var_1'] = $some_val;
$_SESSION['var_2'] = $some_other_val;

php- session_start(); . , , , . "" $_COOKIE db .

+1

Use the $ _SESSION variables. When a user logs into your site, save the variable in the global variable $ _SESSION.

session_start();
$_SESSION['user'] = $/* user */;

In another php script you can install;

session_start();
$user = $_SESSION['user'];
0
source

All Articles