Jump to content

[SOLVED] Guest Online Script...


phpSensei

Recommended Posts

I modified it a bit from RedArrow, and it seems to work. I don't know what I changed but I just fixed the errors, and it actually echos the number of guests online.

 

 

Anything Wrong?

 

<?php


function online_users($period = 300 /* seconds */) 
{ 
    $query = "DELETE FROM `online_users` WHERE `updated` < '".(($now = time()) - $period)."'"; 
    $result = mysql_query($query) or die ($query.' -- '.mysql_error()); 
    $query = "INSERT INTO `online_users` (`ip`, `updated`, `user`) ". 
             "VALUES ('".$_SERVER['REMOTE_ADDR']."', '".$now."', '".(isset($_COOKIE['user'])?1:0)."')". 
             "ON DUPLICATE KEY UPDATE `updated`='$now', `user` = '".(isset($_COOKIE['user'])?1:0)."'"; 
    mysql_query($query) or die ($query.' -- '.mysql_error()); 
    $query = "SELECT * FROM `online_users`"; 
    $result = mysql_query($query) or die ($query.' -- '.mysql_error()); 
    $return = array('users' => 0, 'guests' => 0); 
    if(mysql_num_rows($result) > 0) 
    { 
        while($row = mysql_fetch_assoc($result)) 
        { 
            if($row['user'] == 1) 
            { 
                $return['users']++; 
            } 
            else 
            { 
                $return['guests']++; 
            } 
        } 
    } 
    return $return;     
} 

?> 

<?php 

# in use 
$online = online_users(); 
$s['users'] = ($online['users'] != 1) ? 's' : NULL; 
$s['guests'] = ($online['guests'] != 1) ? 's' : NULL; 

#print results 
echo 
$online['guests'].$s['guests'];


?> 

Link to comment
Share on other sites

You should do:

 

$query = "INSERT INTO `online_users` (`ip`, `updated`, `user`) ". 
             "VALUES ('".$_SERVER['REMOTE_ADDR']."', '".$now."', '".(isset($_COOKIE['user'])?1:0)."')". 
             "ON DUPLICATE KEY UPDATE `updated`='$now', `user` = '".(isset($_COOKIE['user'])?1:0)."'"; 
    mysql_query($query) or die ($query.' -- '.mysql_error()); 

 

on the login page anyways...thats just my opinion

 

Link to comment
Share on other sites

Or just simply have a new field for the data table

 

`online` bigint(12) unsigned NOT NULL default '0'

 

 

then as a user logs in, put this on the login page after you write a code that grabs that user's ID:

 

$now = time()

 

$query = "UPDATE users SET online = '$now' WHERE userID  = '$userID'";

$result = ...bla bla

 

---------------------------------------------------------------------

 

then on the whatever page...to count how many users are online:

 

$now = time();

$last_five = time() - 300;

 

$query = "SELECT * FROM users WHERE online > '$last_five' AND online < '$now'";

$result = ...bla ..bla

 

That should work

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.