PHP Statistics online number of numbers combined MySQL stats online number

Source: Internet
Author: User
    1. CREATE TABLE Ccol (
    2. ID integer NOT NULL auto_increment, #记录的ID
    3. IP char (a) is not NULL, #访问者的IP地址
    4. Dtstamp datetime NOT NULL, #最后访问时间
    5. Uri Char (255), #访问者请求的URI
    6. Primary KEY (ID)
    7. );
Copy Code

Then, write the PHP code for the statistics

  1. /*

  2. Files: ccol.php
  3. Objective: To count the number of simultaneous online visitors
  4. edit:bbs.it-home.org
  5. */
  6. $duration=1800;
  7. Require "db.php"; Prepare yourself, database access classes
  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 for more than half an hour
  13. $ccol->query ("select * from Ccol WHERE ip= ' $remote_addr '");
  14. Determine if the current IP exists in the table
  15. if ($CCOL->NF ())//have?
  16. {
  17. $ccol->next_record ();//Move down the pointer to the array of records found
  18. $ID=$CCOL->F (' id ');
  19. $ccol->query ("UPDATE ccol SET Dtstamp=now (), uri= ' $request_uri ' WHERE id=$id");
  20. Set last access time and access 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 half an hour, and the WHERE clause in the back is optional--the time Out has been deleted
  28. $ccol->next_record ()
  29. echo "Online number:", $ccol->f (' Ccol ');
  30. $ccol->free_result ();
  31. ?>

Copy Code

Call the method and call the program on top of 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.