how to check the number of users actually online (even if they come close your browser)?
A problem that often occurs when you want to control the number of users online on its website, is to detect if the user has exited the closing the browser directly to our site.
There is no way to detect the event that you have left our site taken to another site, or the user may be directly released by closing the browser.
then I present a solution to this problem, made with ASP and Ajax.
First, I explain the theory, then shows you the code (very simple). If someone wants to skip the explanation, in fondo trovate lo zip con gli script.
Io ho risolto il problema prendendolo al contrario: non sono io che devo vedere se l'utente è online..o se è uscito...o se ha cambiato sito... Ma è l'utente che deve segnalarmi di essere ancora sul mio sito.
Quindi ogni tot minuti, secondi o quello che vorremo, il nostro utente, con una richiesta asincrona fatta con ajax, comunicherà al nostro server, di essere ancora sul sito. E quando segnala la sua presenza, mi salverò il minuto della sua segnalazione....a questo punto, se l'ultima segnalazione di un utente dovesse essere più vecchia del numero di minuti che io imposto, elimino semplicemente l'utente dalla mia variabile che contiene gli utenti reali sul sito.
theoretically the game ends!
course, between saying and doing ...
The first operation is typical to set the variable "application" that will contain information on users online that interest me.
The Application variable (for the uninitiated, is a variable common to all users, and that remains in effect until all users have not come from my site), is typically created in global.asa or (in ' ASP (global.asa is the first file that is read the entry on the site)), or we can decide where and how we want to create it. I in this example, the variable application, which I call "user" when I create a registered user of my site to login. (if you want you can create even more, with the reference to the user his sessionID)
example we will create a generic system that counts all users accessed the site, not just those that have logged , then keep as a reference point the sessionID of the user. The sessionID is a session variable, which is created automatically by the server every time you enter a user is unique and lasts for as long as the user running on our website. This variable're ready to be useful, for example, in ecommerce sites, to fill the temporary shopping cart, without the user has not registered or has logged.
First we create the variable application, se non esiste ancora, oppure accodarci alla variabile già esistente:
Per il codice di questa prima parte, aprire il file "test.asp", all'interno dello Zip.
Il risultato sostanzialmente sarà un array con "SessionID---minuto della segnalazione della propria presenza o della propria entrata"
A questo punto, in ogni pagina del mio sito, avrò un piccolo script, che tramite l'oggetto XMLHttpRequest, che è la base per le richieste asincrone di Ajax, che con un intervallo fissato da noi, segnalerà la nostra presenza, ed andrà ad aggiornare l'orario nella variabile application.
Nella pagina test.asp, trovate il codice Ajax che aggiorna la nostra presence, referring to the page "presenza.asp", and reads the current list of users through the 'user-online.asp.
The table of online users is then inserted, again with an asynchronous request, the test page.
On the "user-online.asp", which is the page that I count the people, and I generate the list, I have the script that checks if the users in the application variable, they reported their presence in recent n minutes, I have set.
If I set two minutes, when the difference between the actual and the minutes of the last report of presence is greater than 2, the user is deleted.
This solution obviously works when the user exits without the typical Logout, because it is no longer on the site, most will not report its presence ... and then it will be removed from the Application variable.
In the zip you will find 4 pages:
- Test.asp: that contains the code to be included in the initial entry page
- presenza.asp: This is the page where its presence is signaled
- online.asp-users: it is page that generates the table with the list of users, which counts users, and eliminates any people left.
- out-Test.asp: the page for the output type Logout.
If you want proof, go to this page actually users online (maybe access with two or three browsers at the same time as the entry of several simulated users)
If you want to download the zip file click here>> really Users online by alessio_1976
The script works with IE from 7 up, Firefox, Google Chrome, Safari. (tested by me)
I hope it will be useful, and if someone has to make improvements and changes, so be it!
Warning!
updated on: 14/01/2010
Added: Many people have asked me how to use the script to display the username of the user. Add a second
Zip, which contains the script changed, so that it is used the session variable that contains Username of the users accessing the site, instead of the variable that contains the Id of the session, the previous script.
in the zip file there are a few more that are used to simulate a login:
- "login.asp" which has the form to get
- "create-session.asp" that creates the session variable called "username", which is then used in the script, to keep track of user presence. Obviously, my advice is always to use the user id, and then with that, get any usernames to be displayed on the site.
I am posting the link to go before the tests: check username really online
I am attaching the zip here ... Milanos: users-real-online
0 comments:
Post a Comment