Newbie can't get session data to work :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gxpark
Forum Commoner
Posts: 33
Joined: Fri Jun 21, 2002 4:01 pm
Location: Mexico City

Newbie can't get session data to work :(

Post 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! :)
ibelimb
Forum Newbie
Posts: 18
Joined: Wed Jun 19, 2002 2:59 pm
Location: New York, USA

Post 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
gxpark
Forum Commoner
Posts: 33
Joined: Fri Jun 21, 2002 4:01 pm
Location: Mexico City

Post 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 :)
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post 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");
gxpark
Forum Commoner
Posts: 33
Joined: Fri Jun 21, 2002 4:01 pm
Location: Mexico City

Post by gxpark »

Thank you! Thank you! Thank you! Thank you! Thank you! Thank you!

:D :D :D
Post Reply