Php code for counting online users combined with mysql-php Tutorial

Source: Internet
Author: User
Php code used to count the number of online users combined with mysql to count the number of online users

  1. Create table ccol (
  2. Id integer not null auto_increment, # Record ID
  3. Ip char (15) not null, # visitor's ip address
  4. Dtstamp datetime not null, # Last access time
  5. Uri char (255), # the visitor's requested URI
  6. Primary key (id)
  7. );

Then, compile the php code for statistics.

  1. /*

  2. File: ccol. php
  3. Objective: to count the number of concurrent online users
  4. Edit: bbs.it-home.org
  5. */
  6. $ Duration= 1800;
  7. Require "db. php"; // self-prepared, database category
  8. // Contains DBSQL
  9. $ Ccol = new dbSQL;
  10. $ Ccol-> connect ();
  11. $ Ccol-> query ("delete from ccol WHERE (UNIX_TIMESTAMP (NOW ()-UNIX_TIMESTAMP (dtstamp)> $ duration ");
  12. // Delete records that have exceeded half an hour
  13. $ Ccol-> query ("SELECT * FROM ccol WHERE ip = '$ REMOTE_ADDR '");
  14. // Determine whether the current IP address exists in the table
  15. If ($ ccol-> nf () // is there?
  16. {
  17. $ Ccol-> next_record (); // move down the pointer to the record array found
  18. $ Id = $ ccol-> f ('id ');
  19. $ Ccol-> query ("UPDATE ccol SET dtstamp = now (), uri = '$ REQUEST_URI' WHERE id = $ id ");
  20. // Set the last access time and page
  21. }
  22. Else // No
  23. {
  24. $ Ccol-> query ("insert into ccol VALUES (0, '$ REMOTE_ADDR', now (), '$ REQUEST_URI ')");
  25. }

  26. $ Ccol-> query ("select count (*) AS ccol FROM ccol WHERE (UNIX_TIMESTAMP (NOW ()-UNIX_TIMESTAMP (dtstamp) <= $ duration ");

  27. // Find the record in the last 30 minutes. The WHERE clause can be left blank. the record that exceeds the specified time has been deleted.
  28. $ Ccol-> next_record ()
  29. Echo "online users:", $ ccol-> f ('ccol ');
  30. $ Ccol-> free_result ();
  31. ?>

Call method: Call this program on each page of the site. For example:

  1. -- Index. php
  2. ...

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.