Web Hosting Talk







View Full Version : How to show how many people online?


jamesyap
12-07-2002, 08:48 AM
I have a Sarah Michelle Gellar fan site that received serve 1000+ visitors a day. So what I am thinking is I want to show people how many people are currently online. So what script could I use?

James Yap

Chachi
12-07-2002, 08:51 AM
You should have posted this in the Programming forums, but anyway here's answer to your question:

<<MOD NOTE: Moved>>

(from spoono.com)

You have to put this on everypage you want it to appear.

It can be broken down into 2 sections.

Use PhpMyAdmin :)

Alright log on to PhpMyAdmin and make a new database called "users". We are going to insert 3 fields into a new table called useronline called timestamp, ip, and file. Lets dump the following code into the Users database using the form on the PhpMyAdmin page:

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)
);

Alright Now lets create the PHP script. Its rather large and lets hope you can follow along. Here is what we'll do in English:

1. Give the server info.
2. Get the time.
3. Insert the values of the person thats online into the mySQL, then create the Failed response if it failed.
4. Do the same thing but delete the user from the database when he leaves
5. Grab the results.
6. Output the results.

Finally, here is the PHP Code:

<?php
//fill in some basic info
$server = "localhost";
$db_user = "username";
$db_pass = "password";
$database = "users";
$timeoutseconds = 300;

//get the time
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

//connect to database
mysql_connect($server, $db_user, $db_pass);

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}

//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}

//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!($result)) {
print "Useronline Select Error > ";
}

//number of rows = the number of people online
$user = mysql_num_rows($result);


//spit out the results
mysql_close();
if($user == 1) {
print("$user user online\n");
} else {
print("$user users online\n");
}
?>

Toolz
12-07-2002, 09:42 AM
In IIS/ASP you'd put a coulple of lines of code in your global.asa file - specifically the application start and end and session start and end.

Will give you just the total not the full list as per sophisticated example above.

phpa
12-07-2002, 09:50 AM
This is a programming discussion, but as this response was posted here, please also be aware that this is a pooly designed solution for a number of ways, not least because it's inefficient. You may wish to modify it along the lines below.

If you wish to just count the number of IP's then use the SQL count() feature to return a single row with the count of IP's rather than all the rows themselves. e.g. SELECT COUNT(DISTINCT ip) ...

Note also that this is counting the users hitting per page, rather than the site as a whole. You may wish to change PHP_SELF to the SERVER_NAME, for example.

You may additionally want to skip the delete query, and make the select count() with a where condition that's also based on the timestamp. This has the benefit of increased performance, and preserving a record in the database of accesses that you may find useful for other analyses. You may choose to use a cron job to fire off a query that flushes old data once or several times a day.

A further technique is to avoid the database entirely and make use of shared memory. PHP has two functions for storing and retriving variables from shared memory, shm_put_var and shm_get_var. You could store an associative array that's keyed by IP address, and store the time of access as the value. Keep that array in shared memory, retrieve it on each page, add/update the user hitting the page to the array, scan for IP's to remove because they're stale, and the count of items left are the active users. Write back the array to SHM. You may wish to use semaphores around the accessing for synchronisation.

Good luck.

UH-Matt
12-07-2002, 11:19 AM
hmmmmmmmmmmm sarah :D

phpa
12-07-2002, 11:31 AM
;)