FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Close The Page With Php





gerimter
<body onUnload="<?PHP code ....>" ></body>
What should I do if I wrote does not work when the page is closed?
Peterssidan
The PHP code is executed on the server before the user receives the page. It doesn't care if the text outside the PHP-tags are html or something else.

When the browser receives the page it can't see that it was generated with PHP. You can have onUnload call a JavaScript function that is executed by the browser (on the user's computer). If you want it to run some code on the server you will have to use JavaScript to load a page that will execute the PHP code.
LxGoodies
The remarks are correct.

As PHP is working server side and the complete document is already prepared in PHP, you won't get any client events.

That is, in the normal case.

You could open another document on OnUnLoad (Peterssidan's suggestion), but this will disturb what the user is doing.

Below example uses Javascript and a (nice and silent) Ajax notification to circumvent the problem.

Whenever you leave the document by choosing a new document, or click <BACK> the server side script setgone.php will be invoked.

Disadvantage: nothing happens if the user just quits the browser. The Javascript OnUnload will not be executed, at least in Firefox it won't. I did not test this for other browsers. For internet explorer there exists a solution (see this link )

Smile Lx

Code:
<script language="Javascript">
function TellServer()
   {
      var req = null;

      
      if(window.XMLHttpRequest)
         req = new XMLHttpRequest();
      else if (window.ActiveXObject)
         req  = new ActiveXObject(Microsoft.XMLHTTP);
      req.onreadystatechange = function()
      {
         
         if(req.readyState == 4)
         {
            if(req.status == 200)
            {
               document.ajax.output.value=req.responseText;   
            }   
            else   
            {
               document.ajax.output.value="Argh!";
            }   
         }
      };
                req.open("GET", "setgone.php", true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send("string="+"aha");
   }
function UnloadProc()
{
  TellServer();
}
</script>
<html>
<body onunload="Javascript:UnloadProc()">
</body>
</html>
LxGoodies
Let's return to this one,

Peterssidan wrote:
You can have onUnload call a JavaScript function that is executed by the browser

LxGoodies wrote:
Disadvantage: nothing happens if the user just quits the browser. The Javascript OnUnload will not be executed, at least in Firefox it won't. I did not test this for other browsers. For internet explorer there exists a solution (see this link )

This afternoon, I found a website that uses another solution for Firefox and Chrome. It's Microsoft ! Go open outlook.com/yourstuff, then unplug your modem cable. Now try to leave the page by quitting.. The top page disappears and a white Chrome window remains.

Appearently, outlook.com opens the top window as a popup, equal size (covering). When the popup is closed, the bottom window can relay the Ajax stuff to the server, to log off. Then, it quits.

Smile Lx
Related topics
PHP Toturail
Web e-mail !
make up a Suspended list
[php] including from lib folder impossible
Getting a URL with PHP
Can frames be a good choice? What then?
PHP VS ASP
comment integrer une page html dans pcnuke
integrer une animation flash dans php
comment integrer le script googleadsense dans une page php
comment integrer un script dans une page php
load file into div dynamically using html possible
Frame Effect Without Frames! (PHP)
IE8 not allowing new random image on each page (PHP and CSS)
PHP error page, die()
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.