Jump to content

Session Time out


Fuzzy Logic

Recommended Posts

Hey,

 

What i am trying to achive is, when a user has been incative for a certein time (For arguements sake 10 minutes) it auto logs them out.

I have already played around with some code, but it always seems to log out right away.

 

Within index.php i have a simple login script with config.php included into it.

 

config.php is included into every single page, as it is my main function file and also has my databse connection within it.

 

Within config.php i start the users session with

session_start()

.

Also within config.php is code to inmput into the suer databse table their current time active.

 

So my problem and therefore question is, how can i make it so no mater what page they are on (passed index.php) they can not be inactive for more than 10 mintes.

 

Any help would be great, I have the feeling I am overthinking it, and the problem is dead easy =)

 

-Fuzzy.

Link to comment
Share on other sites

in you config page put a current time stamp and add 10 minutes to it, add this into a session variable, then everytime the page gets loaded you can then use config to check the time stamp variable, if it is within 10 minutes reset it to the new time stamp plu 10 mins if it is past 10 mins then kill the session variables and redirect to login page.

Link to comment
Share on other sites

in you config page put a current time stamp and add 10 minutes to it, add this into a session variable, then everytime the page gets loaded you can then use config to check the time stamp variable, if it is within 10 minutes reset it to the new time stamp plu 10 mins if it is past 10 mins then kill the session variables and redirect to login page.

 

Can you expand on this at all, maybe with a quick example?

Link to comment
Share on other sites

that's probably what dragon_sa meant:

<?php
session_start();
if(isset($_SESSION['timestamp']) && (time() - $_SESSION['timestamp'] > 60 * 10)){ // 10 minutes
    session_destroy();
    session_start();
    session_regenerate_id();
}
$_SESSION['timestamp'] = time(); 

?>

 

note: I did not check whether the code actually works, but it should :P

Link to comment
Share on other sites

@Fuzzy Logic: Yes, that's exactly why I did it that way.

 

@Daniel0: The only "issue" is, that the cookie will expire, but not the session itself. So the session is still available, if you know the SID. But your method should work flawlessly in most cases of course, also it's much more simple.

Link to comment
Share on other sites

Ugly PHPSESSID? Please excuse my ignorence =(

 

The session must have a name, by default it's PHPSESSID. You can pass the session ID to PHP in two ways:

1) Using a cookie with that name.

2) Using a parameter in the URL with that name, e.g. index.php?PHPSESSID=987sd9fshfblablabla

 

You can turn off #2, which I would recommend.

Link to comment
Share on other sites

Ugly PHPSESSID? Please excuse my ignorence =(

 

The session must have a name, by default it's PHPSESSID. You can pass the session ID to PHP in two ways:

1) Using a cookie with that name.

2) Using a parameter in the URL with that name, e.g. index.php?PHPSESSID=987sd9fshfblablabla

 

You can turn off #2, which I would recommend.

 

Ah, okay thanks =)

 

I got it to work thanks you, using that script posted Thank you =)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.