Page 1 of 1

Session Timeout Problem

Posted: Thu Sep 13, 2007 1:44 am
by waqas_punjabian
hi all,

I am facing 2 problems regarding Sessions.

problem1:
I have created a session variable $_SESSION['first'] on first file, "session1.php".
Then I print its value on another page named "session2.php".
I need this variable's value to be expired or destroyed after 30 seconds automatically.
For that purpose I've changed a setting in php.ini as under

Code: Select all

session.gc_maxlifetime = 30
Here comes the problem, I've waited for 35 seconds on "session2.php", then refreshed the page but alas! I still get the value of Session variable. Can anybody tell me what I am doing wrong here? Or should I have to do some thing else ?

problem2:

I wanted to change the session time out value by code, so, I used following code to change the time in "set_session.php" file.

Code: Select all

ini_set('session.gc_maxlifetime',25);
This is also not working for me.
When I go to some other file e.g) "session2.php" it shows me the time which is set in php.ini, it did not change the time using php code.

can any body tell me how can i do that ?

the code for all of three files are as under:

session1.php

Code: Select all

<?
	session_start();

	$currentTimeoutInSecs = ini_get("session.gc_maxlifetime");
	print("TimeOut: ".$currentTimeoutInSecs." sec<br>");

	$_SESSION['first']="Value of First Session";
	print("sess val : ".$_SESSION['first']);
?>
<br />
<br />
<a href="session2.php">session2</a> | <a href="set_session.php">set session</a>
session2.php

Code: Select all

<?
	session_start();

	$currentTimeoutInSecs = ini_get("session.gc_maxlifetime");
	print("TimeOut: ".$currentTimeoutInSecs." sec<br>");

	print("sess val : ".$_SESSION['first']);
?>
<br />
<br />
<a href="session1.php">session1</a> | <a href="set_session.php">set session</a>
set_session.php

Code: Select all

<?
	session_start();

	ini_set('session.gc_maxlifetime',30);
	
	$currentTimeoutInSecs = ini_get("session.gc_maxlifetime");
	print("TimeOut: ".$currentTimeoutInSecs." sec<br>");

	print("sess val : ".$_SESSION['first']);
?>
<br />
<br />
<a href="session1.php">session1</a> | <a href="session2.php">session2</a>

Posted: Thu Sep 13, 2007 7:51 am
by Begby
Why do you need this to expire so quickly?

It might be better if you also store a timestamp in your session so that it doesn't affect the rest of your site, 30 seconds is way to short for most uses of session variables. Maybe like this

Code: Select all

session_start() ;
$_SESSION['first']['val']="Value of First Session";
$_SESSION['first']['created'] = time() ;
Then check it on another page

Code: Select all

session_start() ;

if ((time() - $_SESSION['first']['created']) > 30)
{
  unset($_SESSION['first']);
  echo '"first" has timed out';
}

Posted: Fri Sep 14, 2007 1:46 am
by waqas_punjabian
Thanks Begby,

This clue has solved my problem, because my application was in testing phase. And I had to test the behavior of the system when session times out, so, I wanted it to be expired quickly so that I could test it further, and now I got the solution. Once again, thanks 4 your help.

But still can anybody tell me how to set the session timeout by code? because

Code: Select all

ini_set('session.gc_maxlifetime',30);
is not working for me. And what is the problem in it? Why it is not being set here ?


regards,
Waqas

Posted: Fri Sep 14, 2007 2:07 am
by s.dot
I'd imagine you have to call ini_set() before you call session_start().
Nevermind, I was wrong.

Code: Select all

C:\Users\HP_Administrator>php -r "session_start(); ini_set('session.gc_maxlifeti
me', 30); echo ini_get('session.gc_maxlifetime');"
30