Jump to content

Using time to work out Online Status


gaza165

Recommended Posts

<?php

include("dbconnect.php");


$sql = mysql_query("SELECT last_activity,username FROM users");

echo "<ul class='online'>";
while($row = mysql_fetch_assoc($sql)) {

$usertime = strtotime($row['last_activity']);
$currenttime = time();

echo "usertime is ".$usertime;
echo "<br/>current time is ".$currenttime."<br/><br/>";

if($usertime < $currenttime)
{
echo "IDLE";
} else {

echo $row['username']."ONLINE";
}
}
echo "</ul>";


?>

 

The first thing i do is get the current time, i then compare that with the users last activity and if it is less than the current time, i know the user is idle, however if the last activity is greater than the current time then we know the user is online.

 

I also am running another script everytime a message it set to update the users last activty to the current time * 100 so it puts the time in the future so that we know the user is still online and not idle..

 

<?php
session_start();

include('dbconnect.php');
$username = $_SESSION['login']['username'];
$sql = mysql_query("UPDATE users set last_activity = CURRENT_TIMESTAMP * 100 WHERE username = '$username'");

?>

 

can someone tell me where I am going wrong with this script, what have i missed?

 

thanks

 

Garry

Link to comment
https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/
Share on other sites

<?php

include("dbconnect.php");


$sql = mysql_query("SELECT last_activity,username FROM users");

echo "<ul class='online'>";
while($row = mysql_fetch_assoc($sql)) {

$usertime = strtotime($row['last_activity']);
$currenttime = time();


if($usertime < $currenttime)
{
echo $row['username']."IDLE<br/>";
} else {

echo $row['username']."ONLINE<br/>";
} else 

if($usertime < ($currenttime * 60 * 10)) {

echo $row['username']."OFFLINE";
}

}
echo "</ul>";


?>

 

I get  'Parse error: parse error in C:\XAMPP\htdocs\Shoutbox\config\guests_online.php on line 21'

change this...

<?php
if($usertime < $currenttime)
{
echo $row['username']."IDLE<br/>";
} else {

echo $row['username']."ONLINE<br/>";
} else 

if($usertime < ($currenttime * 60 * 10)) {

echo $row['username']."OFFLINE";
}

}

to...

<?php
if($usertime < $currenttime)
{
echo $row['username']."IDLE<br/>";
}elseif($usertime < ($currenttime * 60 * 10)) {

echo $row['username']."OFFLINE";
} else {

echo $row['username']."ONLINE<br/>";
}

$offlinetime = $usertime - time() * 60 * 10;

if($usertime < $currenttime)
{
echo $row['username']."IDLE<br/>";

}elseif($usertime < $offlinetime) {

echo $row['username']."OFFLINE";

} else {

   echo $row['username']."ONLINE<br/>";
}

nope this is what ive got

 

<?php

include("dbconnect.php");


$sql = mysql_query("SELECT last_activity,username FROM users");

echo "<ul class='online'>";
while($row = mysql_fetch_assoc($sql)) {

$usertime = strtotime($row['last_activity']);
$currenttime = time();

$offlinetime = $usertime - time() * 60 * 1;

if($usertime < $currenttime)
{
echo $row['username']."IDLE<br/>";

}elseif($usertime < $offlinetime) {

echo $row['username']."OFFLINE";

} else {

  echo $row['username']."ONLINE<br/>";
}

}
echo "</ul>";


?>

 

it works between ONLINE and IDLE just not offline!!

<?php
session_start();

include('dbconnect.php');
$username = $_SESSION['login']['username'];
$sql = mysql_query("UPDATE users set last_activity = CURRENT_TIMESTAMP + 100 WHERE username = '$username'");

?>

 

this code is rang everytime a new message is sent to show that the user is still online!!

I really cannot guage anything from this as it is ASP....

 

What do you think is the best thing to update the time with everytime a message is sent.

 

update.php

<?php
session_start();

include('dbconnect.php');
$username = $_SESSION['login']['username'];
$sql = mysql_query("UPDATE users set last_activity = CURRENT_TIMESTAMP WHERE username = '$username'");

?>

 

guestsonline.php

 

<?php

include("dbconnect.php");


$sql = mysql_query("SELECT last_activity,username FROM users");

echo "<ul class='online'>";
while($row = mysql_fetch_assoc($sql)) {

$usertime = strtotime($row['last_activity']);
$currenttime = time();


if($usertime < $currenttime)
{
echo "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.