开发者

An equivalent of javascript unload for PHP

For javascript there is an unload function, when a page closes, do something. Is the开发者_Python百科re the same for php?

Thanks Jean


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.


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.


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');


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.


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. ?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜