Jump to content

Need Help With Time Script


gaza165

Recommended Posts

I am trying to write a script to let me see who is online, who is idle and who is offline. This is the script I have so far.

 

<?php
session_start();
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']);

$difference = $usertime - time();
$idletime = $difference - 150;

if($difference < -500)
{
echo $row['username']." is offline<br/>";
} else

if($idletime <= 0)
{
$idletime++;
echo $row['username']." is idle<br/>";
} else {

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

echo "--------------------------------<br/>";


}


?>

 

Everytime a message is sent, i run the script below to update the users last activity timestamp ahead so it knows there has been activity from the user so get it to say they are online.

 

After an amount of inactivity i want it to say they are 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'");

?>

 

The problem I am having is when the user sends the message, im not sure how much i have to update the time by for the countdown to start again.

 

Thanks

 

Garry

Link to comment
Share on other sites

Why are you trying to mess with future times?  Simply set the last_activity to be NOW().  Then all you need to do is decide what the threshold of time is for you to consider the users to be Online/ Idle/ Offline.

 

Your $difference should be $time - $usertime.  This will be a value in seconds.  It's up to you to decide how many seconds in the past activity indicates that they are online vs. Idle vs Offline.

 

 

Just to raise the question -- so if I don't post I'm not "online"?  Seems to me that online/offline usually is about login to a system.

Link to comment
Share on other sites

Ok....

 

The reason for the ONLINE/IDLE/OFFLINE thing is that it is a Chat Application written in JQuery/PHP. Much like the popular facebook, when a user has been inactive for a certain amount of time, it shows that user to be idle.

 

Then after a longer amount of time, it times the user out and they need to log in again. This is what I am trying to achieve here.

 

I have updated the code to this..

 

 

userstatus.php

 

<?php
session_start();
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']);

$difference =time() - $usertime 
$idletime = $difference - 150;

echo $difference." is the difference";

if($difference < -500)
{
echo $row['username']." is offline<br/>";
} else

if($idletime <= 0)
{
$idletime++;
echo $row['username']." is idle<br/>";
} else {

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

echo "--------------------------------<br/>";

}


?>

 

 

then the

 

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'");

?>

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.