Page 1 of 1

session lost....

Posted: Mon May 29, 2006 9:31 pm
by zinc
everything works well when i develope the codes under fedora core 4..using php 5.1.1 apache 2.2.0

but when i migrate to a window 2003 server, php 5.1.2, all the session variables are lost when the browser is redirected from the login verification to the main page...

the codes are as follows

Code: Select all

session_start();
session_register('session');
I included this at the top of the main page so that i can use the stored session variables.

Code: Select all

$session['id']=session_id();
$session['userid']=$rec['name'];
$session['loginID']=$loginID;
$session['password']=$password;
session_write_close();
This is how i assigned the session variables.

After verifying the user is a valid user, i redirect him using javascript

Code: Select all

print "<script>self.location='../myinbox/myinbox.php'</script>";
At the top of the inbox page, i checked if one of the session variables is NULL. If its NULL, i will redirect him to the login page.
In this case, i'm using the session variable to check. But after logging in, the session variables should be set after the user had logged in. But in this case, all the session variables are lost. I had searched many sites for similair problems but none really gave a valid answer.
Thx for the help.

Posted: Mon May 29, 2006 9:45 pm
by Christopher
First, I think you need to declare $session before registering it. Second, the prefered way is to use the superglobal $_SESSION instead which is automatically registered. You can use it in exactly the same way as your $session var without all the potential global/register vars problems.

Posted: Mon May 29, 2006 9:49 pm
by zinc
sorry, i don't quite understand your first point.

As for the second point that you mentioned, i did try using

$_Session['loginID'] = $loginID

but it still doesn't work.

Posted: Mon May 29, 2006 10:52 pm
by RobertGonzalez
session_register is not a prefered way of assigning session vars anymore. That functionality has been replaced with the assignment of the vars into the $_SESSION superglobal array. After calling session_start() you can just assign session vars by throwing them into the $_SESSION global.

Code: Select all

<?php
session_start();

if (isset($POST['username']))
{
    $_SESSION['user'] = $_POST['username'];
}
?>

Posted: Tue May 30, 2006 12:23 am
by zinc
i tried using $_Session but it still doesn't work....

has it got to do with the php.ini configuration file??

Posted: Tue May 30, 2006 12:36 am
by Christopher
It's not $_Session, it is $_SESSION. Variable names in PHP are case sensitive.

Posted: Tue May 30, 2006 12:50 am
by zinc
my mistake.

miss out on the uppercase.

By the way, why is it that the old codes work on LINUX but it doesn't work on WINDOW???

Posted: Tue May 30, 2006 3:56 am
by twigletmac
Different php.ini configuration probably.

Mac