tags:

views:

63

answers:

6

Hello,

For javascript there is an unload function, when a page closes, do something. Is there the same for php?

Thanks Jean

+4  A: 

PHP is server side, so by the time your user sees the page, the PHP thread is already done. You could of course put an ajax call in your javascript unload though that calls a PHP script.

UltimateBrent
Or I could use a javascript, but then, I have will have to recode certain part of the PHP to JS
Jean
You wouldn't have to recode anything with this approach, the PHP script is still being called.
Robert
A: 

PHP is not event-oriented, the best way to know if a page finished is in the last line.

If you need to know when a class is "unloaded" you can use the function __destroy

Rafael SDM Sierra
That would be for session destory
Jean
If you destroy the session manually, you can write you code right after calling the function. But PHP does not have a way to call a function when a session is destroyed because it depends on a large set of factors, like cookie expiration, manual cookie removal, etc
Rafael SDM Sierra
Are you thinking of __destruct()?
DexterW
A: 

Since PHP is a server side language it's not possible to do something like this without using some sort of browser based scripting language.

Munzilla
+2  A: 

PHP is a server-side technology. It has no idea that the page is closing unless you use JavaScript to send a message to a PHP script. Then it's just as any other php page.

Joel Etherton
hmmm.........That is something I have to try..let me give it a go
Jean
Great...worked................
Jean
+2  A: 

If you want to execute something at the end of your request, you can use register_shutdown_function:

function my_func() {
  // perform some cleanup
}    

// my_func will be called after the rest of your script has executed
register_shutdown_function('my_func');
meagar
+1 Beat me by 10 seconds. Deleting mine.
webbiedave
@webbiedave I re-read the question and I don't think this answer is relevant.
meagar
Assuming when I close the page, or move from a particular page
Jean
this is when the script has finished compiling, not when the user closes the page.
RobertPitt
@meagar. Seems not. Oh well.
webbiedave
A: 

short answer NO!

the reason for this is that PHP just creates a page, ones it has built up the page it sends the html to Apache to serve, when this is done, the PHP script is already finished.

A possible hack is using javascript.

so heres and example, Requires jQuery¬

$.fn.unloadping = function(url,params,callback)
{
     this.onbeforeunload = function()
     {
         $.ajax({
             type : 'GET',
             data : params,
             success : function(response)
             {
                 callback(this) //Send window context back
             }
         });
         return false;
     }
     return $(this); //keep the chain
}

and can use like so

$(window).unloadping('/ping/closewindow.php',{page:window.location.href},function(context){
    this.close(); //Needs to be checked
});

can i ask why you would like to do this? what's your motives etc. ?

RobertPitt