Run mysql query on page exit / go from page?

can someone tell me if it is possible to run a mysl query that will be triggered if the user goes from the page or leaves the page?

for example, I have a table called sessions, which I use "session_start" and "session_end" to try to track users on my site. therefore, in order to reach the "session_end" mark, I wanted to know if it is possible to update this column for a user leaving the site?

There are other ways to achieve this that I don’t think about, I would really appreciate any suggestions,

early

+4
source share
2 answers

If this site has a login script / log out script, you can

  • When the user finishes the session (logs out), you can run a refresh request with the username of the user's session and timestamp
  • If the user’s session time (perhaps he leaves the website but doesn’t leave it), you can run a query with the username and timestamp in order to have data when the user timeout and session duration for.
  • If a user logs in to this session, the username / user name entered will store the session data in the database.

These are just some of the ways to do this (the first two ways that come to mind). Take a look at PHP sessions for further assistance. Also for the query material, see PHP PDO Again, I want to repeat that these are the first two things that come to mind, and with the Information I provided, I came up with the following recommendations. Hope this is some help / "AH - HAH!" moment. Enjoy sir.

Change 1

If you accidentally try to run a request when the browser is closed / the user clicks a link to an external domain, then no, because the commented comment cannot trust the browser to notify the server. But, as I wrote above the editing, you could use the above methods to get closer to what you requested.

Also be sure to read the OWAPS PHP security cheat sheet . This and the PHP user manual should be your best friends: D

Again, I hope this will be useful to you!

Happy coding :)

+3
source

1: logging in and logging out - saving the page ID (visited page) and time for tracking user logout (end of session)

2: Ajax script on each page - updates the page identifier and timestamp every few seconds or minutes, if the update does not mean that the user is unavailable / left

3: php code on each page, saves the IP address, page identifier and timestamp for each visit (if there is no new entry, the user is unavailable)

0
source

All Articles