session timeout and redirect

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
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

session timeout and redirect

Post by neridaj »

Hello,

How do I redirect a user when the session has timed out? It never seems like my sessions timeout e.g., I can always comeback and resume where I left off instead of being redirected when a session has timed out. I know the default time is 20 minutes, but I don't know how to implement a redirect based on that.

Thanks for any help,

Jason
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: session timeout and redirect

Post by aceconcepts »

The default time is 24 minutes and you will be able to check whether a session has expired or has not been set by:

Code: Select all

 
if(!isset(session_id()))
 
//or
 
if(!isset($_SESSION['loggedIn']) || $_SESSION['loggedIn']!=1) //where $_SESSION['loggedIn'] is set to 1 when a user successfully logs in
 
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Re: session timeout and redirect

Post by neridaj »

I tried that and I'm always immediately redirected when I hit the page.

Code: Select all

session_start();
$_SESSION['valid_user'] = $username;
if(!isset($_SESSION['valid_user']))
    header('Location:http://www.blah.com');
    die("Your session has expired!");
Seems simple enough but...

Thanks,

Jason
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: session timeout and redirect

Post by aceconcepts »

Try outputting your session variable to see if it contains a value.
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Re: session timeout and redirect

Post by neridaj »

It does in fact contain the value of $username.
Post Reply