Solved

How Can Expire A Session Cookie On The Same Page?

Posted on 2008-09-29
4
211 Views
Last Modified: 2013-11-19
So how do check for the cookie if someone is sitting on the same page (logged in)?
I don't want people logged in sittin on my site doing nothing for more than say 10 minutes...
<?php 

session_start();  
 

$_SESSION['PoolID']="Stuff"; 
 
 
 
 
 

if (!empty($_SESSION['PoolID'])) {   

 setcookie('session', 'active', time()+20);   

} else {

 header ('Location: http://www.google.com');   

 exit;   // exit the current script

}
 
 
 

if (!isset($_COOKIE['session'])) {   

 session_destroy();   // if not, destroy session

 header ('Location: http://www.google.com');   

 exit;     

}
 

echo "<pre>". print_r($_SESSION,true) ."</pre>";
 

?>

Open in new window

0
Comment
Question by:phpretard
  • 3
4 Comments
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22602954
The session cookie for any user in PHP sessions lasts as long as you set it -- no longer.  If you want all the cookies destroyed at some particular time and there are 100 people using your site, you would have to manually change the session cookie time to 1 second at that time -- and that would only stop NEW people logging in -- the current users have their session as long as you previously set.

Explain why this is bothering you, maybe I can help, maybe you misunderstand the issues with sessions.
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22602966
"I don't want people logged in sittin on my site doing nothing for more than say 10 minutes..."

If that is all that is bothering you, just set the session time to 10 minutes, follow the code below -- it MUST be at the very top of each PHP page on your site using the sessions -- the very top -- no exceptions.
<?php

ini_set('session.save_path','sessions');

session_cache_expire(120);

$cache_expire = session_cache_expire();

ini_set('session.cache_expire',10);                         	// in minutes

ini_set('session.gc_maxlifetime',600);                    	// in seconds PHP.INI 24min default

ini_set('session.cookie_lifetime',600); 		// seconds

ini_set('session.gc_probability',100); 			//default is 1

ini_set('session.gc_divisor',100);      			//default value

session_start();

Open in new window

0
 

Author Comment

by:phpretard
ID: 22603524
I have some software that gives false information while monitoring my site when people just leave thier computer and run off to do thier business.  So...if they are looged in and dormat for a set amount of time I would like to redirect them somwhere else.

I don't quite understand the code you wrote above but with just that on one page here's what I get...

Warning: session_start() [function.session-start]: open(sessions\sess_63a629bd37049738b67055af5fc24b5c, O_RDWR) failed: No such file or directory (2) in C:\Inetpub\vhosts\.com\httpdocs\session_timer\timer.php on line 10
 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Inetpub\vhosts\.com\httpdocs\session_timer\timer.php:10) in C:\Inetpub\vhosts\.com\httpdocs\session_timer\timer.php on line 10
 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Inetpub\vhosts\.com\httpdocs\session_timer\timer.php:10) in C:\Inetpub\vhosts\.com\httpdocs\session_timer\timer.php on line 10
 

Warning: Unknown: open(sessions\sess_63a629bd37049738b67055af5fc24b5c, O_RDWR) failed: No such file or directory (2) 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 (sessions) in Unknown on line 0

Open in new window

0
 
LVL 44

Accepted Solution

by:
scrathcyboy earned 500 total points
ID: 22607443
you didn't say you are running on an IIS server, it makes a HUGE diffference to PHP.  I assumed that you were running on linux shared hosting which is where PHP runs flawlessly.  PHP implementations on local MS IIS servers are so much of a hack and difficult to get working right, you have to be sitting in front of the server to debug the problems arising from MS hacks -- as you just saw.

So no, I can't help you remotely debuigging PHP errors on MS IIS -- it is too much hassle.  But if yo noticed my code, the sessions are written to a directory under the root called "sessions".  You have to create that directory first with FULL permissions -- 7 7 7 -- the errors might disappear.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now