PHP Session

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
kerepuki
Forum Commoner
Posts: 30
Joined: Fri Oct 29, 2004 12:28 am

PHP Session

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: PHP Session

Post by hawleyjr »

Are you calling session_start(); at the top of each page?
kerepuki
Forum Commoner
Posts: 30
Joined: Fri Oct 29, 2004 12:28 am

Re: PHP Session

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: PHP Session

Post 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;
}
 
kerepuki
Forum Commoner
Posts: 30
Joined: Fri Oct 29, 2004 12:28 am

Re: PHP Session

Post by kerepuki »

Now it redirects to the default page regardless of whether I have logged in or not.
kerepuki
Forum Commoner
Posts: 30
Joined: Fri Oct 29, 2004 12:28 am

Re: PHP Session

Post by kerepuki »

It is also the exact same configuration on my office computer to my home computer and everything works on my home comp.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: PHP Session

Post 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>';
 
Post Reply