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
https://forums.phpfreaks.com/topic/66565-solved-guest-online-script/
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

 

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

Archived

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

×
×
  • 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.