1

I'm trying to write a script to run when closing the page or tab, but can't get it to work.

Can someone check my code.

Connection_Aborted doesn't seems to work when closing the page.

I don't know whats wrong in my script.

I have search the net for possibility to fix my code. some people put it in while loop which i don't want it to be in while loop can this be done with out loop.

Here is my code.

Ignore_User_Abort(True); 
if(Connection_Aborted()==1)
{
    file_put_contents('/tmp/phptest1234.txt', 'test');
}
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 3
    You cannot detect the user leaving your page. – Niet the Dark Absol Jun 29 '14 at 00:03
  • @NiettheDarkAbsol ofc it is, maybe just not through PHP, but he could send a POST the last thing he does through JS and hope it gets sent, good? No. Safe? Not at all. http://stackoverflow.com/questions/13443503/run-javascript-code-on-window-close Guess you're just not old enough to have lived the happy days of a state web. (Irony) – Thomas Lindvall Jun 29 '14 at 00:08
  • @Thomas Lindvall in JS if the user change or leave the page the script get executed. which i dont want that to happen, i wanted when user close the page only. can this be done with JS ? – user3763747 Jun 29 '14 at 00:14
  • @user3763747 http://stackoverflow.com/questions/1631959/browser-window-close-event it's linked in the first question. – Thomas Lindvall Jun 29 '14 at 00:26
  • @Thomas Lindvall, none of the salutation work for me, what about when the system get switch off ? – user3763747 Jun 29 '14 at 00:54
  • @ Niet the Dark Absol, can you give me example please ? – user3763747 Jun 29 '14 at 00:58

1 Answers1

1

Once the visitor is looking at your page, php has finished running. That's it. You can't do anything else with php alone. Enter Javascript, the option you are looking for. You could try to do an AJAX call to get some php to run if you are truly set on doing whatever it is you want in php.

KnightHawk
  • 901
  • 9
  • 18