troubleshooting Question

PHP MySQL simple active guests display

Avatar of i-CONICA
i-CONICAFlag for United Kingdom of Great Britain and Northern Ireland asked on
3 Comments2 Solutions299 ViewsLast Modified:

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.

$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')");
    $_SESSION['username'] = $_SERVER["REMOTE_ADDR"];
    $user = $_SESSION['username'];
    $date = time();
    mysql_query("INSERT INTO `guests` (username,time) VALUES('$user','$date')");

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros