I am trying to make a counter of visitors to the site, I saw it on a variety of resources, but something was not set.

There is a base in which the number of visitors is stored, there is a php script that should add to the base +1 when the user logs in (this works with the ready event) and -1 when it leaves the site (this is the problem). There is a function that sends an ajax request to this php script, and there is an onbeforeunload or unload that calls the function.

I do not know who is the culprit from this list, but the function manages to work even after the page is closed, at its next loading. That is, the function works only when the page is refreshed or restored ( cntrl + shift + T ) even before the ready event, but if the page is simply closed, then there will be no effect at all. Tested only in firefox , because This browser is a priority.

The code itself:

HTML:

 <script>window.onunload = putout();</script> 

Js:

 function putout() { $.ajax({ type: "POST", url: "./php/process.php", data: { 'function': 'putout', 'def': def, }, dataType: "json", success: function(data) { }, }); } 

PHP:

 include ('./php/connect.php'); mysql_query("UPDATE numb SET numb=numb-1"); 

I also heard that the opera ignores these events altogether, then how can you cross-browser implement the site visitors counter at all?

PS The text has already been repaired a thousand times, this is not at all my style of presentation, take note. In general, I hate the possibility of crawling to third-party people without asking for my publications, this is VERY annoying.

  • Regardless of everything written, the user can simply break the Internet, so even when working on [before] unload, it’s worth starting a timeout and automatically taking the user offline, for example, in about five minutes. And still such things are better for storing in something faster than MySQL. - andreymal
  • On the subject of the question, asynchronous ajax requests when closing a tab do not seem to work, you need to send synchronous ones. - andreymal
  • @andreymal well, suppose, then how to implement using a timeout? If I am not mistaken, then JS works locally, if the user leaves the site, then all his timers will also be lost with him. Start a certain machine with a special php code that will constantly check the early version of the users table with the new one and sift those who are not in the new one? I thought about it, but it seems to be somehow wrong. - Diskyp
  • Exactly what is right. Maybe not exactly (depends on the needs and implementation of the server), but the essence is this. - andreymal

1 answer 1

The method is executed when the page is loaded, but does not work when unloading due to incorrect use.

This code

 window.onbeforeunload = putout(); 

executes the function immediately after loading the script, even before the page is ready. At the same time, no handler will join the event, since the function will return undefined.

To specify a handler for an event, you must remove the method call:

 window.onbeforeunload = putout; 

Now the function will not be called at the start, but will be called before unloading.

In any case, this is an incorrect approach to counting users.

  • Replaced with window.onbeforeunload = putout; - the function completely ceased to be called. - Diskyp