Help(unsolved): set session expire time

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

User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Help(unsolved): set session expire time

Post by raghavan20 »

I am having difficulty finding code to set session to expire after half an hour since last activity.
Last edited by raghavan20 on Wed Jul 27, 2005 3:32 pm, edited 1 time in total.
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post 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();
}
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
br5dy
Forum Newbie
Posts: 22
Joined: Sat Jul 23, 2005 2:52 pm

Post 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.

:D
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post 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]
Last edited by wwwapu on Wed Jul 27, 2005 11:38 am, edited 1 time in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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()
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Wow is it 'fast answer day' today? 8O 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.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Following errors appear

Post 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
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post 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. :D
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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....
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

(Session save config on Windows -- help needed)

Post 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.
Post Reply