Jump to content

seeing if users are online


runnerjp

Recommended Posts

pstats.gif

 

I use a very simple technique:

You are unique by ip.

Check for uniqueness, if NOT then:

check if ((time() - user record's timestamp) > 120 )

if so then:

update user timestamp to time() (current time)

 

Then query for [users] table for records that are >= time() - 120      (that's 2 minutes)

 

ah yes but the problem is if you on a shared network then the ip would be the same would it not?? ur unique by username tho :P also ip adress can be chnaged and stuff and i think can lead to other problems

 

can any tell my wel this works so that it just keeps adding users rather then replacing them

 

<?
$insert = mysql_query("REPLACE INTO `useronline` SET `timestamp`='$timestamp', `ip`='".$_SERVER['REMOTE_ADDR']."', `file`='$filename',`user`='$username',`user_id`='".$_SESSION['user_id']."'") 
or die(mysql_error()); 
?> 

 

for some reason i have the same users information entred every time the go onto the page rather then replacing the existing one :S

humm how can i get this to work??

 

users viewingthread = <?php
http://www.runningprofiles.com/members/index.php?page=message&forum=$forum&id=$id

$result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=message&forum=$forum&id=$id')");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
$onlineuser = $row['user'];
}

if($last_active < 300) {
echo $onlineuser;
}
?>

hey guys...ok i thought the was solved but two things happend today that puzzeld me...

 

first was it beeing this

 

<?php
//Fetch Users Online
$result = mysql_query("SELECT DISTINCT user FROM useronline");
while($row = mysql_fetch_array( $result )) 

$last_active = time() - $row['timestamp'];
$users = mysql_num_rows($result);
if($last_active < 400){
if($users == 1) {
$ol_label = "user";
} else {
$ol_label = "users";
}}
?>    <?php echo "<b>$users</b> $ol_label"; ?> online

 

it echos every one in  the db as beeing online :S

 

also i signed in and fo some reason it added a result in the table that was me but didnt have my username and id then i went to anouther page and it updates me and not the data it just created ?? :S

 

becuase i get the page they where on i was able to track where this happend

 

<?php 
session_start();
require_once '../settings.php';
checkLogin ('1 2');

include "connect.php"; //mysql db connection here
$timestamp = time();
$timeout = $timestamp - 180;
$username= get_username($_SESSION['user_id']);
function selfURL() { 
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; 
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; } 
function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
$filename = (selfURL());
//Insert User
$insert = mysql_query("REPLACE INTO `useronline` SET `timestamp`='$timestamp', `ip`='".$_SERVER['REMOTE_ADDR']."', `file`='$filename',`user`='$username',`user_id`='".$_SESSION['user_id']."'") 
or die(mysql_error()); 
?> 

is there anything there that would make it do that>?

what am i doing wrong here

 

<?php
include '../settings.php';
//Fetch Users Online
$result = mysql_query("SELECT DISTINCT user FROM useronline WHERE timestamp <60");
while($row = mysql_fetch_array( $result )) 

$last_active = time() - $row['timestamp'];
$users = mysql_num_rows($result);
if($users == 1) {
$ol_label = "user";
} else {
$ol_label = "users";
}
?>    <?php echo "<b>$users</b> $ol_label"; ?> online

 

i think im doing this wrong WHERE timestamp <60"

If you store timestamp in seconds you can do it easily.

 

e.g.

 

"INSERT INTO `db_name` . `table_name` ( `user_id`, `timestamp` ) VALUES ( '23', '" . time() . "' )";

 

Then to check the timeout:

 

<?php
// $uid = $_SESSION['user_id']; // Your user ID variable
$sql = "SELECT `timestamp` FROM `db_name` . `table_name` WHERE `user_id` = '" . $uid . "' LIMIT 1"; // timestamp should be an integer (INT)
$res = mysql_query($sql) or die('MySQL Error: ' . mysql_error());
while($row = mysql_fetch_array($res)) {
  $timeout = 5 * 60; // 5 minutes
  if((time() - $row['timestamp']) < $timeout) { // Remember, if big time minus little time is less than timeout
    // do something
  }
  else {
    // do something else 
  }
}
?>

you see thats the thing.... im not getitng one user online as i do this for that

<?php $result = mysql_query("SELECT * FROM useronline WHERE(user='".$gettopic3['author']."')");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
}

if($last_active < 300) {
echo "Online";
} else {
echo "Offline";
} ?> 

 

but i want to get a list of how many users are online within the last minute so i thought it would be something like

 

<?php
include '../settings.php';
//Fetch Users Online
$result = mysql_query("SELECT DISTINCT user FROM useronline WHERE timestamp <60");//i thought i need to get the users that are online from a timelimit here

while($row = mysql_fetch_array( $result )) 

$users = mysql_num_rows($result);//this should display all the users
if($users == 1) {// this i thought would chnage if 1 user on say user if more say users
$ol_label = "user";
} else {
$ol_label = "users";
}
?>    <?php echo "<b>$users</b> $ol_label"; ?> online

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.