Page 1 of 1

PHP Session

Posted: Wed Mar 12, 2008 6:54 pm
by kerepuki
Hi everyone,

I am having a problem with my sessions. I have installed everything and everything is working, producing the correct page and connecting to my db but I am registering a session through a login function which is being written correctly and it is also be registered correctly. However when I try to read the value of the session with $_SESSION['username'] it produces nothing. Its like the session has been registered but it is empty.

This only happens locally. I have uploaded my code to a hosting server and everything works perfectly. Its a bit frustrating as I would like to develop locally and test and produces the same results as I would when my code has been uploaded.

Has anyone any info on what could be causing this?

My session write path is C:\php\session

Thanks

Re: PHP Session

Posted: Wed Mar 12, 2008 7:05 pm
by hawleyjr
Are you calling session_start(); at the top of each page?

Re: PHP Session

Posted: Wed Mar 12, 2008 7:20 pm
by kerepuki
Yeah I am:

session_start();

and then:

if(!session_is_registered("username")){
header("Location: login.php");
}

but the page does not redirect back to the login page so the session is registered but by the looks of things empty. And again, only locally. It works live.

Re: PHP Session

Posted: Wed Mar 12, 2008 8:10 pm
by hawleyjr
kerepuki wrote:Yeah I am:

session_start();

and then:

if(!session_is_registered("username")){
header("Location: login.php");
}

but the page does not redirect back to the login page so the session is registered but by the looks of things empty. And again, only locally. It works live.

Most likely it is with the version of PHP on your local machine.

Try this:

Code: Select all

 
if( isset( $_SESSION['username'] ) ){
     header("Location: login.php");
     exit;
}
 

Re: PHP Session

Posted: Wed Mar 12, 2008 8:15 pm
by kerepuki
Now it redirects to the default page regardless of whether I have logged in or not.

Re: PHP Session

Posted: Wed Mar 12, 2008 8:18 pm
by kerepuki
It is also the exact same configuration on my office computer to my home computer and everything works on my home comp.

Re: PHP Session

Posted: Wed Mar 12, 2008 8:30 pm
by hawleyjr
If it is redirecting then the session var is probably still active. Use the following code to view all session vars:

Code: Select all

 
echo '<HR><PRE>'; print_r($_SESSION); echo '</PRE>';