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
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'

Link to comment
Share on other sites

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/>";
}

Link to comment
Share on other sites

$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/>";
}

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

<?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!!

Link to comment
Share on other sites

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";
}

}


?>

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.