/    Sign up×
Community /Pin to ProfileBookmark

Session Not Timing Out

Hi all,

For the purposes of this example, I am trying to make it so that session data is remembered for 10 seconds between pages. That is, if you click from Page1 to Page2 within 10 seconds, the session data created on Page1 will be available on Page2. However, if you wait more than 10 seconds before clicking from Page1 to Page2, the session data will not be available on Page2.

Here is the code….

session_page1.php

[code]<?
session_set_cookie_params(10);
session_start();
$_SESSION[‘testvar1’] = “apple”;
$_SESSION[‘testvar2’] = “banana”;
$_SESSION[‘testvar3’] = “catfood”;

?>
<html>
<head>
<title>
SESSION TEST – Page 1/2
</title>
</head>
<body>
<a href=”session_page2.php”>Click to go to Page 2 >></a>
</body>
</html>[/code]

session_page2.php

[code]<?
session_start();
$testvar1 = $_SESSION[‘testvar1’];
$testvar2 = $_SESSION[‘testvar2’];
$testvar3 = $_SESSION[‘testvar3’];
?>
<html>
<head>
<title>
SESSION TEST – Page 2/2
</title>
</head>
<body>
<?
echo “testvar1 = “.$testvar1.”<br />”;
echo “testvar2 = “.$testvar2.”<br />”;
echo “testvar3 = “.$testvar3.”<br />”;
?>
</body>
</html>
<?
$_SESSION = array();
session_destroy();
?>[/code]

Thus far, the data is showing up every time!

This is probably a bit of a noob question, but between these forums and php.net I’ve tried a hell of a lot without any success thus far!

Can someone show me hwo to do this?

Any info is very much appreciated!

Cheers,

Chris

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@Peterk92Jan 14.2011 — I have never had to mess with session timeout's but have you tried putting "" around the 10 or tried the function session.cookie_lifetime("10") ?
Copy linkTweet thisAlerts:
@Chris_JacksauthorJan 15.2011 — Hi Peter,

Thanks for the reply. No such luck though I'm afraid.

I have tried the following, both with and without double-quotes...
ini_set('session.gc_maxlifetime', 10);
ini_set('session.cookie_lifetime', 10);
session_set_cookie_params(10);


session.cookie_lifetime("10") on its own throws an error.

Any other ideas folks?


Cheers,

Chris
Copy linkTweet thisAlerts:
@NogDogJan 16.2011 — I think a more robust approach would be to set a $_SESSION value with a timestamp that you can check, e.g.:

first page:
[code=php]
<?php
session_start();
$_SESSION['last_access'] = time();
[/code]

other page:
[code=php]
<?php
session_start();
if(!isset($_SESSION['last_access']) or time() - $_SESSION['last_access'] > 10) {
$_SESSION = array(); // clear all session data
}
[/code]
Copy linkTweet thisAlerts:
@Chris_JacksauthorJan 16.2011 — NogDog, thanks mate! You've done it again!

I was looking at my previous solution "with blinkers on". Your solution is much better!

Now that I have this working it leads me to a quick followup query...


There are some pages in my site where I'll want a short timeout, but there are going to be some where I want an 8 hour timeout.

If I do the following:ini_set('session.gc_maxlifetime', 0);
ini_set('session.cookie_lifetime', 0);


...will this set the system timeout to infinite... thus allowing me to use your suggested code in ALL pages (for both short and long timeouts)?


Cheers mate,

Chris
Copy linkTweet thisAlerts:
@NogDogJan 17.2011 — The cookie lifetime of 0 will be until the user loses his browser. gc_maxlifetime is different though, and 0 would mean 0 seconds. Note that the gc stuff is a bit convoluted, in that it also depends on the probability and divisor settings as to whether or not the garbage collection (that's the "gc") even gets performed on any given page access. Also, as long as only one user is accessing the pages, the garbage collection won't apply to their data, as it only gets run when session_start() is called by a script, but [i]after[/i] the user's session data file gets "touched" and has its modification time updated (and thus will only clean up expired data for [i]other[/i] users).

So basically, I'd probably not mess around with the gc settings unless you need to make them longer, and let the cookie expiration and any timestamp stuff you want to use in $_SESSION to control the timeouts (or even user access data in the DB if that makes more sense).
Copy linkTweet thisAlerts:
@Jarrod1937Jan 17.2011 — For your particular application why not just store a unix timestamp within the user's session, then on the next page request check if the timestamp is not older than 10 seconds, if it is then call session_destroy().

Edit: Wow, somehow missed Nogdogs post above (post #4). His is basically what i am suggesting, sessions aren't designed for the type of timeouts you're trying to do, so a custom timestamp is about the only way.
Copy linkTweet thisAlerts:
@Chris_JacksauthorJan 17.2011 — Cool, thanks Nogdog! That's a great explanation of the other attempts I was making. I'll just set the cookie lifetime to 0 and store the timestamp in a session var as suggested.

hehe, thanks for the input anyway Jarrod!

Much appreciated


Cheers lads,

Chris
×

Success!

Help @Chris_Jacks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...