Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 5th, 2003, 10:17 AM
Registered User
 
Join Date: Jul 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default session_destroy on exit??

Is it possible to do an session_destroy when someone closes the window?

I already tried to use:
<body OnUnload="<? session_start; session_destroy; ?>">

but it doesn't work..

Somebody got a better id?

With kind regards,
Harmen Wessels
 
Old July 5th, 2003, 04:51 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Actually it is correct not to work,
You're trying to destroy the session client-side with JavaScript when this can only be done server-side.

My suggestion is use your onUnload=event() event handler to open a self-destructing pop-up window that will call on the session_destroy() function.

I'm not sure of how the JavaScript goes, but it may be something simular to this...

<script language="JavaScript" type="text/javascript">
<!--

function sessiondestroyer() {

window.open("session_destroyer.php","","WIDTH=425, HEIGHT=445")

}

//-->
</script>
<body onUnload="sessiondestroyer()">

And then your session_destroyer.php script will call on the neccessary server-side PHP script to destroy sessions.

I would add an onload() event handler that called on the self.close() function to automatically get rid of the window and not annoy your surfers.

hth
: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 5th, 2003, 09:41 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

In addition to my former statement,
You can only call on session_start() at the very beginning of your PHP script, before any header information is outputted. The reason for this is the PHP session handler will automatically create a cookie with the session ID as its value, and consequently cookies may only be sent out before any other output as part of the HTTP headers. The best way to handle the session_start() function is to place it before all code as the very first statement in your script. I'm not sure if the session_destroy() has the same requirements, but to be safe I would treat it similarly in your script architecture. It makes more sense to destroy the session before output and near the start of your script, otherwise you could still be calling on session variables in other places of your script where you might not want to and cause some hard to track down bugs.

If you need any further help with sessions just ask! I would be happy to help out - and there are certainly many others in the group that can aid in your quest!

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning on session_destroy() surya PHP How-To 2 February 10th, 2009 06:17 AM
How to exit from loop dishant XSLT 2 October 6th, 2008 03:34 AM
on exit mohiddin52 Access VBA 2 July 10th, 2007 09:28 AM
how to exit from function try.test.abc C# 2 January 11th, 2007 06:24 AM
exit from for-each preethi.s XSLT 1 September 8th, 2006 05:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.