Jump to content

Recommended Posts

I've made a login and register script (and changed it thanks to the guys here pointing out a huge flaw in the last version).

 

I tried my hand at making a little Online/Offline script for the member list I have. I used a column in the database (originally named "online"), if it's set to 1, which it is when you login through:

<?php
mysql_query("UPDATE `XXX` SET `online` = '1' WHERE `username` = '".$_SESSION['username']."' AND `password` ='".$_SESSION['password']."'");
?>

This works all well and good, but when a user doesn't logout, using the logout button, then they still shown as online on the member list (when they aren't). I'm really not sure how to make a better version of this.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/54197-member-online/
Share on other sites

One way would be to possibly have a script that ran at the top of every page that did 2 things.

 

1 would be to update the "online" field to the current timestamp (instead of 1)

2 would be to update the table and set all "online" fields to NULL if they had been "inactive" for x minutes

Link to comment
https://forums.phpfreaks.com/topic/54197-member-online/#findComment-268006
Share on other sites

I would suggest using $_SERVER['REMOTE_ADDR'] this will provide you with access to the users IP then insert a timestamp in a table. Use this as an include on everypage. You could also use an fwrite() function to write to a text file and avoid using mysql all together.

 

<?PHP

$time= time() - 300;

$ip= $_SERVER['REMOTE_ADDR'];

$sql= "SELECT * FROM logged_on WHERE time > '$time'";

$result= mysql_query($sql)or die('Error: '.mysql_error());

While($row= mysql_fetch_array($result, MYSQL_ASSOC){

IF($ip == $row['ip'])

  $time= time();
  $sql= "UPDATE logged_on SET time='$time' WHERE ip='$ip'";

}else{
  
  $time= time();
  $sql= "INSERT INTO logged_on SET time='$time', ip='$ip'";

}


$online= mysql_num_rows($result);

echo "Users online: $online";



$time= time();

$sql= "INSERT INTO logged_on SET ip='$ip', time='$time'";
$result= mysql_query($sql)or die('Error: '.mysql_error());

?>

 

Link to comment
https://forums.phpfreaks.com/topic/54197-member-online/#findComment-268075
Share on other sites

Would this code work in a custom function?

 

Actually. Looking through that code. I'm a little confused, there's a } missing.

 

<?php $onlinetime   = time() - 300;
$onlineip     = $_SERVER['REMOTE_ADDR'];
$onlinesql    = "SELECT * FROM `logged_on` WHERE `time` > '$time'";
$onlineresult = mysql_query($onlinesql);

while($onlinerow = mysql_fetch_array($onlineresult, MYSQL_ASSOC)
{
if ($onelineip == $onlinerow['ip'])
  {
   $onlinetime = time();
   $onlinesql  = "UPDATE `logged_on` SET `time` = '$onlinetime' WHERE `ip` = '$onlineip'";
  }
else
  {
   $onlinetime = time();
   $onlinesql  = "INSERT INTO `logged_on` SET `time` = '$onlinetime', `ip` = '$onlineip'";
  }
}

$isOnline     = mysql_num_rows($onlineresult);
$isOnlineEcho = "Users online: $isOnline"; ?>

Is this correct?

Link to comment
https://forums.phpfreaks.com/topic/54197-member-online/#findComment-268326
Share on other sites

personally... i'd do it more like so...

 

<?php
$timeout=time()-300;
$query=mysql_query("SELECT `id` FROM `users` WHERE `timestamp`>'$timeout' AND `loggedin`='1'");

while($row=mysql_fetch_assoc($query)){
mysql_query("UPDATE `users` SET `loggedin`='0' WHERE `id`='$row[id]' LIMIT 1");
}

$query=mysql_query("SELECT * FROM `users` WHERE `id`='$_SESSION[id]' LIMIT 1");
$user=mysql_fetch_assoc($query);
if($user[loggedin]==0){
$_SESSION[error][]='Your session has expired, log in again';
redirect('index.php?op=logout');
}
?>

 

then when you want to find who's online...

 

$query=mysql_query("SELECT `username` FROM `users` WHERE `loggedin`='1'");
while($row=mysql_fetch_assoc($query)){
$online[]=ucwords($row[username]);
}
$online=implode(', ',$online);

Link to comment
https://forums.phpfreaks.com/topic/54197-member-online/#findComment-268354
Share on other sites

personally... i'd do it more like so...

 

<?php
$timeout=time()-300;
$query=mysql_query("SELECT `id` FROM `users` WHERE `timestamp`>'$timeout' AND `loggedin`='1'");

while($row=mysql_fetch_assoc($query)){
mysql_query("UPDATE `users` SET `loggedin`='0' WHERE `id`='$row[id]' LIMIT 1");
}

$query=mysql_query("SELECT * FROM `users` WHERE `id`='$_SESSION[id]' LIMIT 1");
$user=mysql_fetch_assoc($query);
if($user[loggedin]==0){
$_SESSION[error][]='Your session has expired, log in again';
redirect('index.php?op=logout');
}
?>

 

then when you want to find who's online...

 

$query=mysql_query("SELECT `username` FROM `users` WHERE `loggedin`='1'");
while($row=mysql_fetch_assoc($query)){
$online[]=ucwords($row[username]);
}
$online=implode(', ',$online);

Would you mind explaining how this would work?
Link to comment
https://forums.phpfreaks.com/topic/54197-member-online/#findComment-268396
Share on other sites

sorry... forgot... after it grabs the account...

 

$time=time();
mysql_query("UPDATE `users` SET `timestamp`='$time' WHERE `id`='$_SESSION[id]'");

 

sure... firstly it says... any user that is online... but hasnt changed pages within 5 minutes, automatically gets logged out... then... you just grab the accounts that have a loggedin=1 and put them into a nice shiney list :-)

Link to comment
https://forums.phpfreaks.com/topic/54197-member-online/#findComment-268408
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.