Link to home
Start Free TrialLog in
Avatar of i-CONICA
i-CONICAFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of i-CONICA

ASKER

Thanks so much guys. Really appreciated.