Backend determines whether the user closes the browser (closes all tab related to the site)

Source: Internet
Author: User

A) procedure steps

1, JS write a timed request back end (PHP), the backend received a request to, the current timestamp write to the file

2, PHP blocking, here I write 3 seconds, that is, sleep (3)

3, get the current time and the time in the file for comparison, if the time interval is 3 seconds, it means that the front end has not been requested, as the user closed all the site-related tab

II) Realization principle

First of all, every request comes in, immediately update the timestamp in the file, and then block the program for 3 seconds;

Well, if I set the front-end of the timing request is 2 seconds, the second request came over, in fact, the first request has not been processed (because blocked for 3 seconds);

When the first request blocking the end, down execution, the time stamp in the file has been modified by the first request, then the current time to reduce the time stamp in the file, the interval is certainly not 3 seconds;

Conversely, if the interval is reduced to 3 seconds, it means that the file timestamp has not been modified, that is, there is no next request, as the user closed the site link, then can do log records such as Logout

c) part of the Code

Js:

functionTest (URL) {varXMLHTTP =NewXMLHttpRequest (); Xmlhttp.onreadystatechange=function(){        if(xmlhttp.readystate==4){            if(xmlhttp.status==200){                //Console.log (xmlhttp.responsetext);}}} xmlhttp.open ("GET", URL,true); Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Xmlhttp.send (NULL);} Window.setmyinterval=function(func, interval) {varNexttime =interval; varStart =NewDate (). GetTime (); varnow =NULL; varToffset = 0; vari = 0; varGoGoGo =function() {window.settimeout (function() {i++; now=NewDate (). GetTime (); Toffset= Now-(Start + i *interval); Nexttime= Interval-Toffset;            Func ();        GoGoGo ();    }, Nexttime);    }; GoGoGo ();}vari = 0; Window.setmyinterval (function() {Test (' Test ' + ((i%2) +1) + '. php '); I++;}, 2000); Test (' test2.php ');

Php:

test1.php

<? PHP file_put_contents  Time ()); Sleep (3); $t 1                file_get_contents (' User1_time.txt '); $now             Time (); if ($now$t 1 = = 3)    {error_log("SUCCESS", 3, "Debug.txt")    ; unlink (' User1_time.txt ');}? >

test2.php

<? PHP require (' test1.php ');? >

I think of the way to do that, modify it according to the actual requirements of the project

Finish

Backend determines whether the user closes the browser (closes all tab related to the site)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.