Session Timeout Problem

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
waqas_punjabian
Forum Commoner
Posts: 67
Joined: Wed Aug 10, 2005 9:53 am

Session Timeout Problem

Post 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>
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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';
}
waqas_punjabian
Forum Commoner
Posts: 67
Joined: Wed Aug 10, 2005 9:53 am

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply