Session doesn't work in PHP

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
homerwells
Forum Newbie
Posts: 4
Joined: Fri Mar 21, 2003 9:26 am

Session doesn't work in PHP

Post by homerwells »

I'm beginning to think that session handling just doesn't work in PHP;
This is my script:

<?

function InitializeSession()
{
$HTTP_SESSION_VARS["logged"] = true;
}

InitializeSession();

echo $HTTP_SESSION_VARS["logged"];
echo $_logged;

?>

It prints NOTHING. Does anyone have any idea of what the hell is going on?
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Try replacing $HTTP_SESSION_VARS with $_SESSION to call sessions.

You might also want to remove the underscore between the $ and logged.

Then again, I'm no pro. Just my two measly bits. ;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you've got PHP 4.1 or above try (as Jim said):

Code: Select all

session_start(); // you must have this at the top of the script

function InitializeSession() 
{ 
     $_SESSION['logged'] = true; 
} 

InitializeSession(); 

echo $_SESSION['logged'];
If however you're using PHP 4.0.6 or below you might be able to do (this may not work cause it's flaky):

Code: Select all

session_start(); //'cause you can't do sessions without it

function InitializeSession() 
{ 
     global $HTTP_SESSION_VARS;  // you have to declare this global in order to use it
     $HTTP_SESSION_VARS['logged'] = true; 
} 

InitializeSession(); 

echo $HTTP_SESSION_VARS['logged'];
otherwise you're going to have to look into using session_register().

Mac
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

..and on the first page the var is set you probably nee a

session_register('logged');

as well..
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Stoker wrote:..and on the first page the var is set you probably nee a

session_register('logged');

as well..
Although $HTTP_SESSION_VARS and $_SESSION do not play nicely with session_register() - you kinda have to use one or the other, not both. If you can, use $_SESSION.

Mac
Post Reply