showing # of online guests?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Patriot
Forum Commoner
Posts: 34
Joined: Tue Jun 18, 2002 1:36 pm

showing # of online guests?

Post by Patriot »

can anyone help me with this?
i dont have a register/ login system, so i only want it to display the guests.
i also dont have access to any mysql, so it needs to be php
User avatar
massiveone
Forum Commoner
Posts: 29
Joined: Tue Jun 18, 2002 4:39 pm
Location: Canada
Contact:

the code to do the job

Post by massiveone »

Here is the code you can use all you need is mysql and php installed on your server
:D


<?php

$Session_name = "default";
$host = "self explanatory";
$username = "put user name here";
$password = "db passowrd";
$database = "database";
$table = "online_users"; //make a table called online_users

if ($Session_name == "default") {
session_start();
}
else {
session_name("$Session_name");
session_start("$Session_name");
}

$SID = session_id();
$time = time();
$dag = date("z");
$nu = time()-900;

mysql_connect ($host, $username, $password) OR DIE ("Could not connect to MySQL");
mysql_select_db($database) OR DIE ("Can't select database.");

$sidcheck = mysql_query("SELECT count(*) FROM $table WHERE SID='$SID'");
$sid_check = mysql_result($sidcheck,0);

if ($sid_check == "0") {
mysql_query("INSERT INTO $table VALUES ('$SID','$time','$dag')");
} else {
mysql_query("UPDATE $table SET time='$time' WHERE SID='$SID'");
}

$count_users = mysql_query("SELECT count(*) FROM $table WHERE time>$nu AND day=$dag");
$users_online = mysql_result($count_users,0);


mysql_query("DELETE FROM $table WHERE time<$nu");
mysql_query("DELETE FROM $table WHERE day != $dag");

mysql_close();

if ($users_online == "1") {
echo "You are alone to view this page right now.\n";
}
else {
echo "There's $users_online people viewing this page right now.\n";
}

?>
pacco
Forum Newbie
Posts: 2
Joined: Tue Jun 18, 2002 5:42 pm
Location: Toronto, Canada
Contact:

Post by pacco »

Since the author does not have database access, a database solution wont help him!

Since the web is stateless and you are not using a login system, you wont really have any way of knowing whether a user has left your site or closed his browser, but you can identify unique IP addresses hitting your site over a short time period. My solution to your problem involves keeping a list of IP addresses that are hitting your site, making sure not to add duplicate entries, and remembering to expire each entry (remove it from the list) after a certain period of time (20 minutes is good I'd say). Counting the number of IP's in the list at any time will tell you how many users are looking at your site (or were looking at it within the last 20 minutes).

To do this, you'll need to use the Filesystem functions (check the manual for fopen(), fclose(), fputs() and fgets() functions). You can access the requesting client's IP address in $_SERVER['REMOTE_ADDR'].

Best way would be to check for the existence of the IP List file before trying to write to it. If it doesnt exist, create it. Then seek through the file trying to find the current client's IP. If it's not in the list, add it to the list along with a timestamp. If the client's IP is in the list already, update the timestamp. After doing all that for the current client, remove/expire any IP's in the list with timestamps more than 20 minutes old. After all this, simply total up the number of entries in the list, and that number is how many people are looking at your site!
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Patriot
Forum Commoner
Posts: 34
Joined: Tue Jun 18, 2002 1:36 pm

didnt work :(

Post by Patriot »

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in S:\Inetpub\sites\smyr\targetroot\user.php on line 27

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in S:\Inetpub\sites\smyr\targetroot\user.php on line 36
There's people viewing this page right now.


i put that exact code in, but it gave me this^
thanks pacco, i actually do have mysql, but i only wanted to do it as a last resource.
i dont really have enough time to log every 20 minutes and research how to....but i appreciate your help.
Post Reply