Monday, March 22, 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