PHP session variables not working correctly
Posted: Tue Mar 10, 2009 6:52 am
I have 2 pages one which checks the sql database for existence of a username and password and then creates a session variable and then one page which checks for the existence of this variable before proceeding.
I am new to PHP so I would appreciate your patience but have been using asp/asp.net for about 10 years so I do have a good grasp of concepts.
This is the code which checks the username and password and tries to create the session variable:
I have put an echo directly after this line ($_session['name'] = $info['uname']
to verify that the variable is being created properly, and below is the code which I want to verify if the session variable has been created successfully. But at the moment it redirects back to the login screen as if the variable is blank, I put in echo $_session['uname'] after session_start(); to see if anything is output but as expected this is blank. Any help is greatly appreciated.
I am new to PHP so I would appreciate your patience but have been using asp/asp.net for about 10 years so I do have a good grasp of concepts.
This is the code which checks the username and password and tries to create the session variable:
Code: Select all
<?
include "connections.php";
include "queries.php";
$dbcnx2;
$info = mysql_fetch_array( $login );
$rows = mysql_num_rows($login);
if ($rows == 0)
header("Location: login.php");
else
{
session_start();
$_session['uname'] = $info['uname'];
header("Location: admin.php");
}
?>Code: Select all
<?
session_start();
if($_session['uname'] == "")
header("Location: login.php");
else
echo "Welcome " . $_session['uname'];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
</body>
</html>