Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How Can Expire A Session Cookie On The Same Page?

Posted on 2008-09-29
4
Medium Priority
?
224 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

916 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