Home > Backend Development > PHP Tutorial > PHP code to count the number of people online combined with mysql to count the number of people online

PHP code to count the number of people online combined with mysql to count the number of people online

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 08:58:59
Original
1250 people have browsed it
  1. CREATE TABLE ccol(
  2. id integer not null auto_increment, #Recorded ID
  3. ip char(15) not null, #Visitor’s IP address
  4. dtstamp datetime not null, #Last access time
  5. uri char(255 ), #URI requested by the visitor
  6. primary key (id)
  7. );
Copy the code

Then, write the php code for statistics

  1. /*

  2. File: ccol.php
  3. Purpose: To count the number of people browsing online at the same time
  4. edit: bbs.it-home.org
  5. */
  6. $duration=1800 ;
  7. require "db.php"; //Prepare yourself, database access class
  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 older than half an hour
  13. $ccol->query("SELECT * FROM ccol WHERE ip='$REMOTE_ADDR '");
  14. //Determine whether the current IP exists in the table
  15. if ($ccol->nf())//Yes?
  16. {
  17. $ccol->next_record();//Move down the pointer of the found record array
  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 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 records within half an hour, the following WHERE clause is optional--out of time Has been deleted
  28. $ccol->next_record()
  29. echo "Number of people online:", $ccol->f('ccol');
  30. $ccol->free_result();
  31. ?>
Copy the code

call method and call this program on every page of the site. For example:

  1. --index.php
  2. ...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template