-
- CREATE TABLE ccol(
- id integer not null auto_increment, #Recorded ID
- ip char(15) not null, #Visitor’s IP address
- dtstamp datetime not null, #Last access time
- uri char(255 ), #URI requested by the visitor
- primary key (id)
- );
Copy the code
Then, write the php code for statistics
-
-
- /*
- File: ccol.php
- Purpose: To count the number of people browsing online at the same time
- edit: bbs.it-home.org
- */
- $duration=1800 ;
- require "db.php"; //Prepare yourself, database access class
- //Contains DBSQL
- $ccol=new dbSQL;
- $ccol->connect();
- $ccol->query("DELETE FROM ccol WHERE (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(dtstamp))>$duration");
- //Delete records older than half an hour
- $ccol->query("SELECT * FROM ccol WHERE ip='$REMOTE_ADDR '");
- //Determine whether the current IP exists in the table
- if ($ccol->nf())//Yes?
- {
- $ccol->next_record();//Move down the pointer of the found record array
- $id=$ccol->f('id');
- $ccol->query("UPDATE ccol SET dtstamp=now(), uri='$REQUEST_URI' WHERE id=$id");
- //Set the last access time and access page
- }
- else//No
- {
- $ccol->query("INSERT INTO ccol VALUES (0, '$REMOTE_ADDR', now(), '$REQUEST_URI')");
- }
$ccol->query("SELECT COUNT(*) AS ccol FROM ccol WHERE (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(dtstamp))<=$duration"); - //Find the records within half an hour, the following WHERE clause is optional--out of time Has been deleted
- $ccol->next_record()
- echo "Number of people online:", $ccol->f('ccol');
- $ccol->free_result();
- ?> p>
-
Copy the code
call method and call this program on every page of the site.
For example:
|