Jump to content

created an online users script


tjhilder

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.