Jump to content

Archived

This topic is now archived and is closed to further replies.

tjhilder

created an online users script

Recommended Posts

Hi,

I created my own online users script but it seems to be duplicating entries and I'm not sure why, maybe someone has an idea by looking at my code?

i've added comments at the end of the queries to show what they do (although you'll probably figure that out without the comments.)

[code]$delete_lookup = "DELETE FROM " . $sql_usersonline . " WHERE (date <= '" . $delete_date . "')"; // delete any entries 5 or more minutes old.
if (!$delete_lookup_result = mysql_query($delete_lookup))
{
    echo "<p>Error: nothing deleted because <b>" . mysql_error() . "</b> the query was: " . $delete_lookup;
}
if(isset($signedin))
{
    $user_lookup = "SELECT ip_address FROM " . $sql_usersonline . " WHERE ip_address='" . $_SERVER['REMOTE_ADDR'] . "'"; // check if user exists
    if ($user_lookup_result = mysql_query($user_lookup))
        {
        if(mysql_num_rows($user_lookup_result) == '1')
        {
            $online_db_query = "UPDATE " . $sql_usersonline . " SET date='" . $online_date . "', name='" . $online_name . "', url='" . $online_url . "', member_id='" . $member_id . "', ip_address='" . $online_ip . "' WHERE ip_address='" . $online_ip . "'"; // if user exists, update it.
            $update_members_visit = "UPDATE members SET last_active='" . $date_var . "' WHERE member_id='" . $member_id . "'"; // updates member table to last visited
            if (!$visit_result = mysql_query ($update_members_visit))
            {
                echo "<p>Member's last active date not updated<!-- because: <b>" . mysql_error() . "</b>.</p><p>The query was $update_members_visit -->.</p>";
            }
        }
        else
        {
            $online_db_query = "INSERT INTO " . $sql_usersonline . " (`date`, `name`, `url`, `member_id`, `ip_address`) VALUE ('" . $online_date . "', '" . $online_name . "', '" . $online_url . "', '" . $member_id . "', '" . $online_ip . "')"; // if not exist, insert it.
        }
        if (!$online_result = mysql_query ($online_db_query))
        {
            echo "<p>Online Users not updated<!-- because: <b>" . mysql_error() . "</b>.</p><p>The query was $online_db_query -->.</p>";
        }
    }
    else
    {
        echo "<p>Error: <b>" . mysql_error() . "</b>.</p><p>The query was $user_lookup.</p>";
    }
}
else
{                          
    $guest_lookup = "SELECT ip_address FROM " . $sql_usersonline . " WHERE ip_address='" . $_SERVER['REMOTE_ADDR'] . "'"; // no user, check if guest exists.
    if ($guest_lookup_result = mysql_query($guest_lookup))
        {
        if(mysql_num_rows($guest_lookup_result) == '1')
        {
            $online_db_query = "UPDATE " . $sql_usersonline . " SET date='" . $online_date . "', name='" . $online_name . "', url='" . $online_url . "', member_id='', ip_address='" . $online_ip . "' WHERE ip_address='" . $online_ip . "'"; // if guest exists, update it.
        }
        else
        {
            $online_db_query = "INSERT INTO " . $sql_usersonline . " (`date`, `name`, `url`, `member_id`, `ip_address`) VALUE ('" . $online_date . "', '" . $online_name . "', '" . $online_url . "', '', '" . $online_ip . "')"; // if guest doesn't exist, insert it.
        }
        if (!$online_result = mysql_query ($online_db_query))
        {
            echo "<p>Online Users, not updated<!-- because: <b>" . mysql_error() . "</b>.</p><p>The query was $online_db_query -->.</p>";
        }
    }
    else
    {
        echo "<p><b>" . mysql_error() . "</b>.</p><p>The query was $guest_lookup.</p>";
    }
}
[/code]

Share this post


Link to post
Share on other sites
Ugh.. don't post entire scripts. It sounds like you might be POSTing to this page multiple times. Your INSERT statement isn't in a loop, so that can't be it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.