Author Topic: How to get Visitors online  (Read 2200 times)

Steve42

  • Newbie
  • *
  • Posts: 1
    • View Profile
How to get Visitors online
« on: June 16, 2009, 08:55:22 AM »
I go to
newbielink:http://www.psoug.org/library.html [nonactive]

near the bottom there is

  ###  visitors online

How do you get the number of Visitors online ?

TIA
SS


Mike

  • Administrator
  • Hero Member
  • *****
  • Posts: 1947
    • View Profile
Re: How to get Visitors online
« Reply #1 on: June 16, 2009, 09:10:44 AM »
Steve,

We record each unique IP adddress that visits us and store it in a table along with a time and date stamp.

On each page load we do a count of the addresses in the table to get that number.

Also on each page load any IP addresses that are more than 10 minutes old are flushed so they don't get counted. The number you see is the number of different visitors in the last ten minutes.

If you'd like the code for this function, I've pasted a similar function in below.


I go to http://www.psoug.org/library.html

near the bottom there is

  ###  visitors online

How do you get the number of Visitors online ?

Code: [Select]
This script displays number of users currently browsing your site.

The table structure:

CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);


The actual Users Online script below should be included in the
footer (or on all pages). It will display the numbers of users
on your website at any one time.

<?php
$server 
"xxx";
$db_user "xxx";
$db_pass "xxx";
$database "xxx";

$db mysql_connect($server$db_user,$db_pass);
mysql_select_db($database,$db);

$timeoutseconds 600//10 minutes

$timestamp time();
$timeout $timestamp-$timeoutseconds;

$insert mysql_query("INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')",$db);
if(!(
$insert)) &#123;
print "Useronline Insert Failed > ";
&
#125;
$delete mysql_query("DELETE FROM useronline WHERE timestamp<$timeout",$db);
if(!(
$delete)) &#123;
print "Useronline Delete Failed > ";
&
#125;
$result mysql_query("SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'",$db);
if(!(
$result)) &#123;
print "Useronline Select Error > ";
&
#125;
$user mysql_num_rows($result);


mysql_close();
if(
$user == 1) &#123;
print("$user user online\n");
&
#125; else &#123;
print("$user users online\n");
&
#125;
?>
« Last Edit: June 16, 2009, 09:20:37 AM by Mike »