Friday, March 26, 2010

Accessing session variables PHP

I am trying to access a session variable from a log in form (using the Log In User Server Behavior) and am lost. I simply want to display the users information in the target page. I read in another discussion that I had to bind the variable to the target page, which I did, but that didn't fix it.

Please help.

Here is my loginFormAction code:

%26lt;?php

// *** Validate request to login to this site.

if (!isset($_SESSION)) {

?session_start();

}

$loginFormAction = $_SERVER['PHP_SELF'];

if (isset($_GET['accesscheck'])) {

?$_SESSION['PrevUrl'] = $_GET['accesscheck'];

}

if (isset($_POST['email'])) {

?$loginUsername=$_POST['email'];

?$password=$_POST['password'];

?$MM_fldUserAuthorization = '''';

?$MM_redirectLoginSuccess = ''owner.php'';

?$MM_redirectLoginFailed = ''register.php'';

?$MM_redirecttoReferrer = false;

?mysql_select_db($database_petriever, $petriever);

?$LoginRS__query=sprintf(''SELECT email, password FROM owners WHERE email=%s AND password=%s'',

?GetSQLValueString($loginUsername, ''text''), GetSQLValueString($password, ''text''));

?$LoginRS = mysql_query($LoginRS__query, $petriever) or die(mysql_error());

?$loginFoundUser = mysql_num_rows($LoginRS);

?if ($loginFoundUser) {

$loginStrGroup = '''';

?//declare two session variables and assign them

?$_SESSION['MM_Username'] = $loginUsername;

?$_SESSION['MM_UserGroup'] = $loginStrGroup; ?

?if (isset($_SESSION['PrevUrl']) %26amp;%26amp; false) {

?$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];

?}

?header(''Location: '' . $MM_redirectLoginSuccess );

?}

?else {

?header(''Location: ''. $MM_redirectLoginFailed );

?}

}

?%26gt;

Accessing session variables PHP

Wherever you want to display the logged-in username simply place this on the first line of a .php page to start the session:

%26lt;?php session_start(); ?%26gt;

Then place this code wherever you want to display the username:

%26lt;?php echo $_SESSION['MM_Username']; ?%26gt;
Accessing session variables PHP

Thanks! That did it!

No comments:

Post a Comment