Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processing session destroy when browser is closed or site changed

Status
Not open for further replies.

CassidyHunt

IS-IT--Management
Jan 7, 2004
688
US
Is there a way to have some final things ran when the browser is closed or the site is changed so I can do some cleanup on a database and sessions?
 
Cassidy-

Not without some hackery. Php is a server side language and thus the only time you have to run your scripts is before the user sees the page.

By default php expires the session when the browser window is closed.

You would be better off to keep your database clean and complete at the end of each script. This also prevents your database from getting corrupted when somebodys computer gets unplugged or out of wireless range or just looses power.

Robert Carpenter
"Disobedience to conscience is voluntary; bad poetry, on the other hand, is usually not made on purpose." - C.S. Lewis (Preface to Paradise Lost)
ô¿ô
 
So php doesn't have a counter part like ASP with the global.asa using the session terminate method?
 
Sorry I cant help you there, I dont know asp well enough.

Now that I think about it you might check out faq434-2037.

Not that you would need to do that task specifically but you might be able to use the way the functions are assigned to the session handlers.

I really doubt that there is a way to easily accomplish what you are seeking.

Robert Carpenter
"Disobedience to conscience is voluntary; bad poetry, on the other hand, is usually not made on purpose." - C.S. Lewis (Preface to Paradise Lost)
ô¿ô
 
Is it possible to look at the what page the user accessed before they came to the site and if that is null or another domain run a script to clean up anything they might have laying around?
 
Session cleanup should be happening with the garbage collection routine.
PHP manual said:
session.gc_probability integer

session.gc_probability in conjunction with session.gc_divisor is used to manage probability that the gc (garbage collection) routine is started. Defaults to 1. See session.gc_divisor for details.
session.gc_divisor integer

session.gc_divisor coupled with session.gc_probability defines the probability that the gc (garbage collection) process is started on every session initialization. The probability is calculated by using gc_probability/gc_divisor, e.g. 1/100 means there is a 1% chance that the GC process starts on each request. session.gc_divisor defaults to 100.
session.gc_maxlifetime integer

session.gc_maxlifetime specifies the number of seconds after which data will be seen as 'garbage' and cleaned up.

Note: If different scripts have different values of session.gc_maxlifetime but shares the same place for storing the session data then the script with the minimum value will be cleaning the data. In this case, use this directive together with session.save_path.

Note: If you are using the default file-based session handler, your filesystem must keep track of access times (atime). Windows FAT does not so you will have to come up with another way to handle garbage collecting your session if you are stuck with a FAT filesystem or any other filesystem where atime tracking is not available. Since PHP 4.2.3 it has used mtime (modified date) instead of atime. So, you won't have problems with filesystems where atime tracking is not available.

The GC should be enough to clean up the sessions. If you want more or earlier cleanup, set the values to a higher probability and short session lifespan.
 
Yes you can. A custom session handler can be easily created - may it be file system based or with a database.
Check out faq434-2037
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top