Page 1 of 2
Help(unsolved): set session expire time
Posted: Wed Jul 27, 2005 9:21 am
by raghavan20
I am having difficulty finding code to set session to expire after half an hour since last activity.
Posted: Wed Jul 27, 2005 9:55 am
by wwwapu
I've done it like this:
Code: Select all
if(isset($_SESSION['time'])){
$time=$_SESSION['time'];
if(($time+1800)<time()){
logout();
}else $_SESSION['time']=time();
}
Posted: Wed Jul 27, 2005 10:42 am
by raghavan20
alright, yours can be used to control the amount of time the user is logged in but I want to set when the session should expire since last activity.
ex: I want the session to expire after 30 minutes of inactivity.
Posted: Wed Jul 27, 2005 10:51 am
by br5dy
You could just make a script included in each page such as "checktime.php", and then inside checktime.php set the time and date (each time), and then make an if statement out of that.

Posted: Wed Jul 27, 2005 11:27 am
by raghavan20
I dont know whether I am explaining it properly to you guys. Anyway, I am telling you again with an example.
ex: If you are using a mailbox, you login and check a few mails and browse other sites and when you come back to the mailbox you may find your session has expired because of session inactivity timeout setting.
I just want the code to work with that.
Posted: Wed Jul 27, 2005 11:31 am
by nielsene
wwwapu's method should work.
On every page you check the timestamp stored in the session of the last view if its more than 30 minutes old you force them to log back in by expiring their login/explicitly logging them out. If they passed the test, you update the last viewed time with the current time.
Posted: Wed Jul 27, 2005 11:34 am
by shiznatix
session_set_cookie_params()
amazing function that is not very widely known but has helped me so very much and is exactally what you are looking for
Posted: Wed Jul 27, 2005 11:35 am
by wwwapu
Repeating myself again.
wwwapu wrote:I've done it like this:
Code: Select all
if(isset($_SESSION['time'])){
$time=$_SESSION['time'];
if(($time+1800)<time()){
logout();
}else $_SESSION['time']=time();
}
If 1800 seconds have passed, then logout(). Else set new 30 minutes period to be used.
Logout being at its simplest
Code: Select all
function logout(){
session_destroy();
}
[EDIT] That session_set_cookie_params() seems great. I think I'm in love...[/EDIT]
Posted: Wed Jul 27, 2005 11:38 am
by shiznatix
wwwapu
your idea wont work if the ini session expire time is less than what they want it to be. if you dont have access to the .ini file as many people dont then that wont work out at all. use the function i mentioned
session_set_cookie_params()
Posted: Wed Jul 27, 2005 11:43 am
by wwwapu
Wow is it 'fast answer day' today?

While I was editing my previous, new answer came.
Session configuration directives are PHP_INI_ALL, so lifetime would not be any problem. But as mentioned session_set_cookie_params() is pure gold.
Following errors appear
Posted: Wed Jul 27, 2005 1:12 pm
by raghavan20
Warning: session_start(): open(D:\Program Files\PHP\sessiondata\sess_8fc6e67ac6f0f4f4326ae524b57b1e6d, O_RDWR) failed: Permission denied (13) in E:\Hosted_Web\LocalUser\omnisoftcouk\website\adminPage.php on line 8
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at E:\Hosted_Web\LocalUser\omnisoftcouk\website\adminPage.php:8) in E:\Hosted_Web\LocalUser\omnisoftcouk\website\adminPage.php on line 8
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at E:\Hosted_Web\LocalUser\omnisoftcouk\website\adminPage.php:8) in E:\Hosted_Web\LocalUser\omnisoftcouk\website\adminPage.php on line 8
Warning: Unknown(): open(D:\Program Files\PHP\sessiondata\sess_8fc6e67ac6f0f4f4326ae524b57b1e6d, O_RDWR) failed: Permission denied (13) in Unknown on line 0
Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (D:\Program Files\PHP\sessiondata) in Unknown on line 0
The code used:
<?
$expiry = time() + (60*30);
session_set_cookie_params($expiry);
session_start();
?>
If the code is wrong, pls give your suggestions
Posted: Wed Jul 27, 2005 1:43 pm
by wwwapu
Did you read first two notes in
manual? do it like
Code: Select all
session_set_cookie_params(30*60);
session_start();
Marvellous, just fantastic.

Posted: Wed Jul 27, 2005 1:44 pm
by nielsene
Well the error messages look like the php.ini is misconfigured so sessions don't work at all.... it has nothing to do with the time-out code....
Posted: Wed Jul 27, 2005 2:01 pm
by raghavan20
I changed the code as 'wwwapu' said but nothing has changed; the same error messages appear again.
Even if I tried with the session_start() command alone, it gives me the same above warning messages.
(Session save config on Windows -- help needed)
Posted: Wed Jul 27, 2005 2:14 pm
by nielsene
We need someone who knows about configuring PHP sessions on Windows to help here.... I'm a Mac/Linux/BSD guy and have little clue what the right advice is here.