does restarting apache reset php sessions?

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

kiwijones
Forum Newbie
Posts: 7
Joined: Thu Feb 26, 2009 1:23 pm

Re: does restarting apache reset php sessions?

Post by kiwijones »

what does it mean if I have no /var/spool/cron/crontabs folder?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: does restarting apache reset php sessions?

Post by Weirdan »

kiwijones wrote:what does it mean if I have no /var/spool/cron/crontabs folder?
Either your users don't have their individual crontabs, or those crontabs are located somewhere else on your system (but likely somewhere around /var/spool).
VladSun wrote:
...cron also reads /etc/crontab, which is in a slightly different format
(see crontab(5)). Additionally, cron reads the files in /etc/cron.d:
it treats the files in /etc/cron.d as in the same way as the
/etc/crontab file
Yeah, those have to be scanned as well.
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: does restarting apache reset php sessions?

Post by inghamn »

Sessions are independent of Apache, even when you are running PHP inside of Apache. I restart Apache all the time without disrupting PHP sessions.

Sessions are stored and garbage collected by PHP. If you're using file-based, the files are stored on the hard drive. Restarting Apache does not clear out the session files.

Unless you're doing some special, cron also has nothing to do with it. PHP is responsible for deleting session files. That's what the gc_ parameters in php.ini control. So, every time you call session_start(), PHP looks for session files on the hard drive (again, this is with file-based storage). Once it finds files, it does a quick scan to see if any session files need to be garbage collected. Then, if the user has sent a SESSID in the request, PHP tries to load the requested session.

I may have the order of operations off a little there, actually. But the point is, sessions will survive even with Apache shut off. (As long as you don't rm them yourself.)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: does restarting apache reset php sessions?

Post by VladSun »

inghamn wrote:Unless you're doing some special, cron also has nothing to do with it.
Well, in fact every PHP installation (on Linux) modifies crontabs. So, while I agree that most of the clean up is done by the GC, there is still some cron jobs involved.
inghamn wrote:PHP is responsible for deleting session files. That's what the gc_ parameters in php.ini control. So, every time you call session_start(), PHP looks for session files on the hard drive (again, this is with file-based storage).
Not exactly - GC will not clean the old sessions on *every* session_start - that depends on session.gc_probability and session.gc_divisor:

Code: Select all

session.gc_probability = 1
session.gc_divisor = 100
so it means there is 1/100 probability that it will check for expired sessions and remove them.

In fact, playing with session.gc_maxlifetime is a PITA when multiple web applications run on a single server, because the GC can not tell which file belongs to a session with shorter/longer lifetime.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: does restarting apache reset php sessions?

Post by inghamn »

VladSun wrote:Well, in fact every PHP installation (on Linux) modifies crontabs. So, while I agree that most of the clean up is done by the GC, there is still some cron jobs involved.
I double checked my installations, and cannot find any PHP stuff in any of my crons. Am I missing something here? I did not think PHP added anything to cron.
VladSun wrote:In fact, playing with session.gc_maxlifetime is a PITA when multiple web applications run on a single server, because the GC can not tell which file belongs to a session with shorter/longer lifetime.
Too true. I was having some troubles with this recently. Now I currently sandbox each application's session files in the bootstrap

Code: Select all

 
    ini_set('session.save_path',APPLICATION_HOME.'/data/sessions');
    ini_set('session.gc_maxlifetime',30*60);
    session_start();
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: does restarting apache reset php sessions?

Post by VladSun »

inghamn wrote:
VladSun wrote:Well, in fact every PHP installation (on Linux) modifies crontabs. So, while I agree that most of the clean up is done by the GC, there is still some cron jobs involved.
I double checked my installations, and cannot find any PHP stuff in any of my crons. Am I missing something here? I did not think PHP added anything to cron.
debian:~# cat /etc/cron.d/php5
# /etc/cron.d/php5: crontab fragment for php5
# This purges session files older than X, where X is defined in seconds
# as the largest value of session.gc_maxlifetime from all your php.ini
# files, or 24 minutes if not defined. See /usr/lib/php5/maxlifetime

# Look for and purge old sessions every 30 minutes
09,39 * * * * root [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -r -0 rm
cat /etc/cron.d/php4
# /etc/cron.d/php4: crontab fragment for php4
# This purges session files older than X, where X is defined in seconds
# as the largest value of session.gc_maxlifetime from all your php.ini
# files, or 24 minutes if not defined. See /usr/lib/php4/maxlifetime

# Look for and purge old sessions every 30 minutes
09,39 * * * * root [ -d /var/lib/php4 ] && find /var/lib/php4/ -type f -cmin +$(/usr/lib/php4/maxlifetime) -print0 | xargs -r -0 rm
Two different Debian servers.
I think crond is used for session files cleanup because it's not guaranteed that PHP interpreter will be executed in a given time period - so, GC could not be called.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: does restarting apache reset php sessions?

Post by inghamn »

Ahh now I understand. Those aren't from PHP, those cron files are installed as part of your distribution. Those are very distribution specific. On all my servers, I compile Apache, MySQL, and PHP from source. Installing PHP from source does not install any cron.
kiwijones
Forum Newbie
Posts: 7
Joined: Thu Feb 26, 2009 1:23 pm

Re: does restarting apache reset php sessions?

Post by kiwijones »

this discussion is fast overwhelming my mental faculties ... i should read up on this sh*t :)

but thanks all the same you guys ... if it wasn't for you, i would never know that deletion of sessions involved probability! :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: does restarting apache reset php sessions?

Post by josh »

on centos, fedora and redhat and possibly others you can do crontab -e to edit the crontab for the current user in the default text editor
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: does restarting apache reset php sessions?

Post by kaisellgren »

Restarting Apache will not clear sessions.

Switching from HTTPD to another will not do it either.

Even changing from Windows to Linux will not do that, if you do the switch properly or the session data is not on that machine.
Post Reply