Page 1 of 1

Newbie can't get session data to work :(

Posted: Fri Jun 21, 2002 4:01 pm
by gxpark
Hi,

I started learning PHP a couple of days ago, and today I've found my first "hard" problem.

Please consider the following code (simplified):

index.php:

Code: Select all

<?
session_start();

include('global.inc');

if (isset($HTTP_GET_VARS&#1111;'login'])) &#123;
	include('login.inc');
&#125;;

echo $User&#1111;'nick'];
?>
global.inc defines the default values for the $User array, but ONLY if the array values aren't set yet:

Code: Select all

if (!isset($User&#1111;'logged'])) &#123; $User&#1111;'logged'] = FALSE; &#125;;
if (!isset($User&#1111;'nick'])) &#123; $User&#1111;'nick'] = 'Stranger'; &#125;;
Later in the file I include a form that sends two values, user and password to login.php?login=1.

So after I submit the form with a username and password, the page is loaded again, this time including the login.inc file:

Code: Select all

<?
$User = DB_GetUser($HTTP_POST_VARS&#1111;'user'], $HTTP_POST_VARS&#1111;'pwd']);
if ($User&#1111;'logged']) &#123;
	$_SESSION&#1111;'User'] = $User;
&#125;;
?>
Here, DB_GetUser returns an array with the following data:

['logged'] - TRUE if user and password match, false otherwise
['nick'] - The name of the user, "Stranger" otherwise

So the first time I submit the form, the echo statement in index.php returns the user name from the database, but if I reload the page, by writing http://localhost/index.php, it prints "Stranger" instead of the user name, hence, the $User array is not being saved in the session...

Any help is appreciated! :)

Posted: Fri Jun 21, 2002 4:07 pm
by ibelimb
Hey,

I cant help ya on that, but wanna help me out? what does the !isset do, does the ! mean not set? or what?

Thanks,
Limb

Posted: Fri Jun 21, 2002 4:11 pm
by gxpark
yes, "!" means "NOT", and isset returns true if a variable is defined, so the "if" condition is "TRUE" when the variable is not set.

Hope that helps :)

Posted: Fri Jun 21, 2002 4:37 pm
by will
i'm not sure if you can manually throw somthing in to the $_SESSION array..

Code: Select all

$_SESSION&#1111;'User'] = $User;
instead, you might try...

Code: Select all

session_register("User");

Posted: Fri Jun 21, 2002 4:45 pm
by gxpark
Thank you! Thank you! Thank you! Thank you! Thank you! Thank you!

:D :D :D