Solved

How Can Expire A Session Cookie On The Same Page?

Posted on 2008-09-29
4
215 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

830 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