Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How Can Expire A Session Cookie On The Same Page?

Posted on 2008-09-29
4
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

719 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