Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute PHP code on page close?

Tags:

php

I am trying to find a method to execute some PHP code once a user closes the page. In my application, once the user closes or navigates away from the page the server will state the user as 'Offline' in the database. This requires the code to know when the user has navigated away from the page. My application also has an endless load (i.e. it will sleep until the user closes the page).

like image 306
Joel Kennedy Avatar asked Feb 01 '11 15:02

Joel Kennedy


People also ask

How do you close a PHP script?

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.

How do I run a PHP script from a website?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

Can I run PHP in a HTML page?

When it comes to integrating PHP code with HTML content, you need to enclose the PHP code with the PHP start tag <? php and the PHP end tag ?> . The code wrapped between these two tags is considered to be PHP code, and thus it'll be executed on the server side before the requested file is sent to the client browser.

Where we can execute PHP code?

A PHP code will run as a web server module or as a command-line interface. To run PHP for the web, you need to install a Web Server like Apache and you also need a database server like MySQL. There are various web servers for running PHP programs like WAMP & XAMPP.


1 Answers

Reliably sending an event when the user closes the page is close to impossible. There is the onbeforeunload event that could do an Ajax call, but that could fail, not be executed for security reasons, or not be executed at all because the user has JavaScript turned off.

This is usually done through session timeouts: It is checked when a request for a certain session was last made. If it was longer than x minutes ago, the session is treated as expired. Is that not an option?

like image 73
Pekka Avatar answered Nov 15 '22 04:11

Pekka