A php code that accurately counts the number of online users

Source: Internet
Author: User
Tags flock
A php code that accurately counts the number of online users

  1. Using filename='online.txt '; // data file

  2. $ Cookiename = 'vgotcn _ onlinecount'; // cookie name
  3. $ Onlinetime = 600; // online validity period, in seconds (that is, 600 equals 10 minutes)
  4. $ Online = file ($ filename );

  5. // The PHP file () function reads the entire file into an array. Similar to file_get_contents (), file () returns the object as an array. Each unit in the array is a corresponding line in the file, including line breaks. If it fails, false is returned.

  6. $ Nowtime = $ _ SERVER ['request _ time'];
  7. $ Nowonline = array ();

  8. // Obtain valid data

  9. Foreach ($ online as $ line ){
  10. $ Row = explode ('|', $ line );
  11. $ Sesstime = trim ($ row [1]);
  12. If ($ nowtime-$ sesstime) <= $ onlinetime) {// if the time is still valid, the data will be saved. Otherwise, the data will not be counted.
  13. $ Nowonline [$ row [0] = $ sesstime; // gets the online list to the array. the session ID is the key name, and the last communication time is the key value.
  14. }
  15. }

  16. /*

  17. @ Create a visitor's communication status
  18. Use cookies for communication
  19. The COOKIE will expire when the browser is closed, but if the browser is not closed, the COOKIE will remain valid until the online time set by the program times out.
  20. */
  21. If (isset ($ _ COOKIE [$ cookiename]) {// if there is a COOKIE that is not the first access, no people are added and the communication time is updated
  22. $ Uid = $ _ COOKIE [$ cookiename];
  23. } Else {// The first access if no COOKIE exists
  24. $ Vid = 0; // initialize the visitor ID
  25. Do {// give the user a new ID
  26. $ Vid ++;
  27. $ Uid = 'u'. $ vid;
  28. } While (array_key_exists ($ uid, $ nowonline ));
  29. Setcookie ($ cookiename, $ uid );
  30. }
  31. $ Nowonline [$ uid] = $ nowtime; // update the current time status

  32. // Count the number of online users

  33. $ Total_online = count ($ nowonline );

  34. // Write data

  35. If ($ fp = @ fopen ($ filename, 'w ')){
  36. If (flock ($ fp, LOCK_EX )){
  37. Rewind ($ fp );
  38. Foreach ($ nowonline as $ fuid => $ ftime ){
  39. $ Fline = $ fuid. '|'. $ ftime. "\ n ";
  40. @ Fputs ($ fp, $ fline );
  41. }
  42. Flock ($ fp, LOCK_UN );
  43. Fclose ($ fp );
  44. }
  45. }
  46. Echo 'document. write ("'. $ total_online .'");';
  47. ?>

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.