Php code used to count the number of online users combined with mysql to count the number of online users
- Create table ccol (
- Id integer not null auto_increment, # Record ID
- Ip char (15) not null, # visitor's ip address
- Dtstamp datetime not null, # Last access time
- Uri char (255), # the visitor's requested URI
- Primary key (id)
- );
Then, compile the php code for statistics.
/*
- File: ccol. php
- Objective: to count the number of concurrent online users
- Edit: bbs.it-home.org
- */
- $ Duration= 1800;
- Require "db. php"; // self-prepared, database category
- // Contains DBSQL
- $ Ccol = new dbSQL;
- $ Ccol-> connect ();
- $ Ccol-> query ("delete from ccol WHERE (UNIX_TIMESTAMP (NOW ()-UNIX_TIMESTAMP (dtstamp)> $ duration ");
- // Delete records that have exceeded half an hour
- $ Ccol-> query ("SELECT * FROM ccol WHERE ip = '$ REMOTE_ADDR '");
- // Determine whether the current IP address exists in the table
- If ($ ccol-> nf () // is there?
- {
- $ Ccol-> next_record (); // move down the pointer to the record array found
- $ Id = $ ccol-> f ('id ');
- $ Ccol-> query ("UPDATE ccol SET dtstamp = now (), uri = '$ REQUEST_URI' WHERE id = $ id ");
- // Set the last access time and 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 record in the last 30 minutes. The WHERE clause can be left blank. the record that exceeds the specified time has been deleted.
- $ Ccol-> next_record ()
- Echo "online users:", $ ccol-> f ('ccol ');
- $ Ccol-> free_result ();
- ?>
Call method: Call this program on each page of the site. For example:
- -- Index. php
- ...
-
|