Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

How Can Expire A Session Cookie On The Same Page?

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
phpretard
Asked:
phpretard
  • 3
1 Solution
 
scrathcyboyCommented:
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
 
scrathcyboyCommented:
"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
 
phpretardAuthor Commented:
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
 
scrathcyboyCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now