And who there at us now on a site...
Probably, travelling in the Network, you not time have come across on similar type an inscription « Now on a site 99 person », that means, that except for you at present this site is looked through by 98 person. Most likely, you wanted to write such thing, but did not know as. Following clause{article} will show as itself to write a script, considering{counting} quantity{amount} of visitors on a site at present, that is online.
In the Internet many the ready scripts, allowing to count users online, but time you read this clause{article}, means, you as well as I not especially like to be picked an another's code. Well, in such, a case, give write such script. But before we shall talk about his{its} advantages and lacks.
Advantages.
The relational database that agree, in our Runet (I have his{its} rather free-of-charge part in a kind) is not necessary for him, is doubtless advantage.
Besides doubtless advantage is speed. I was not too lazy to write a similar script with use of a DB and by means of function microtime () (at the very bottom - result of its{her} job - « Time of generation: … ») has measured execution time:
Using sessions - 0,7 ms
Using a DB - 14,2 ms
That is advantage in speed - more than in 19 times! You can say: « Too to me the big business. Only 13 ms a difference », yes they will be right - and if attendance big? Loading on the server and then really such advantage becomes obvious grows.
One more advantage - that the information is stored{kept} in sessions is not closed yet a browser or 1440 seconds (costs{stands} by default in php.ini), in a DB to you should to delete the information on users manually.
And the last - simplicity to declare session it is enough to call function session_start () and to adjust correct in the image of a DB it is necessary to write the whole 15 lines of a code.
Lacks.
There is no detailed statistics - in can learn{find out} only quantity{amount} of users at present, for conducting statistics (hits, hosts …) it is necessary to work all the same from a DB.
It is necessary PHP4 - as it it is paradoxical - yet all khostery have put to itself PHP4.
Actually a script:
session_start ();
define ("MAX_IDLE_TIME", 3);
function getOnlineUsers () {
if ($directory_handle = opendir (session_save_path ())) {
$count = 0;
while (false! == ($file = readdir ($directory_handle))) {
if ($file! = '.' ** $file! = '..') {
if (time () - fileatime (session_save_path (). ". $file) <MAX_IDLE_TIME * 60) {
$count;
}
}}
closedir ($directory_handle);
return $count;
} else {
return false;
}}
echo ' Users online: '. getOnlineUsers (). '
';
?>
The REMARK: If you do not have special directory for storage of sessions, is simple at once the first line add:
session_save_path ("/path/to/custom/directory");
Now we shall walk by separate parts of a code:
session_start ();
The next line of a code is an announcement of some constant during which users will be considered active, that is present at present on a site. In our example - 180 seconds, that is if pol`zovatel`` has not passed on new page or has not reloaded this during 3 minutes he is considered left from a site and we any more do not take into account it{him}:
define ("MAX_IDLE_TIME", 3);
Now we declare function getonlineuser () and at once we open a "sessional" directory:
function getOnlineUsers () {
if ($directory_handle = opendir (session_save_path ())) {
Further there is a part of a code which actually and is responsible for calculation of users, function returns online kol-in users:
$count = 0;
while (false! == ($file = readdir ($directory_handle))) {
if ($file! = '.' ** $file! = '..') {
if (time () - fileatime (session_save_path (). ". $file) <MAX_IDLE_TIME * 60) {
$count;
}
}
closedir ($directory_handle);
return $count;
} else {
return false;
}
}
And perhaps, it will be reasonable a conclusion of quantity{amount} of users in the end:
echo ' Online of users: '. getOnlineUsers (). '
';

|