• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

PHP MySQL simple active guests display

Hi,

I've got this which I hoped would show how many active guests are visiting the site in the past hour, it's not working though and I'm not sure why.

Also, I don't know how I would remove users after the hour they should be stored for... I don't want the table filling up forever...

Can anyone offer advice on this? I think several parts maybe flawed.

Thanks.
$d = time() - 60 * 60; //last 1 hour
$q = mysql_query("SELECT username FROM `guests` WHERE time > '$d'");
if (mysql_affected_rows($q) > 0) {
    echo "<ul>";
    while ($users = mysql_fetch_array($q)) {
        echo "<li>{$users[0]}</li>";
    }
    echo "</ul>";
}
if (isset($_SESSION['username'])) {
    $user = $_SESSION['username'];
    $date = time();
    mysql_query("REPLACE INTO `guests` (username,time) VALUES('$user','$date')");
}
else{
    $_SESSION['username'] = $_SERVER["REMOTE_ADDR"];
    $user = $_SESSION['username'];
    $date = time();
    mysql_query("INSERT INTO `guests` (username,time) VALUES('$user','$date')");
}

Open in new window

0
i-CONICA
Asked:
i-CONICA
2 Solutions
 
ZylochCommented:
Please try replacing mysql_affected_rows() with mysql_num_rows(). From the manual, the former does not apply to SELECT statements, while the latter does.

As to periodically pruning users, you might consider running a PHP script using a cron job that does this. Another way to do this without cron is to include code on the page so that every time a page is loaded there is a small probability for the pruning code to run. This should keep speed quick enough for users most of the time.

Alternatively, one avenue you may pursue is some way to do this automatically in the database (depending on your database privileges). However, this is not my area of expertise, so I can't tell you if or how this is possible.
0
 
adbrettCommented:
try:
<?php

$one_hour = 60 * 60;
$now = time(); // set time to a varaible so we don't have to keep calling the function
$time_ago = $now - $one_hour; // it's best to use verbose varaible names so it reads well in english


$query = mysql_query("SELECT username FROM `guests` WHERE time > '{$time_ago}'"); // same as yours

if (mysql_num_rows($query) > 0) { // affected rows only works on update/insert/replace/delete, so use num_rows for a query
    echo "<ul>";
    while ($users = mysql_fetch_array($query)) {
        echo "<li>{$users[0]}</li>";
    }
    echo "</ul>";
}

// removed call to time() in both if statements to save on function calls
// moved mysql_query outside if statement to save on code repetition
if (!empty($_SESSION['username'])) {
    $user = $_SESSION['username'];
}
else{
    $user = $_SERVER["REMOTE_ADDR"]; // there's no point setting $_SESSION['username'] here unless you did it for some other reason
}

mysql_query("INSERT INTO `guests` (username, time) VALUES('{$user}', '{$now}')"); // insert date & username into your table
mysql_query('DELETE FROM `guests` WHERE time < {$time_ago}'); // delete anything over 1 hour ago to keep the table clean

?>

Open in new window

0
 
i-CONICAAuthor Commented:
Thanks so much guys. Really appreciated.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now