Jump to content

Recommended Posts

Dear fellow Freaks,

 

So me and my partner have come up with an idea and to build a project for a Runescape Private Server Community.

 

So far, we've managed to build a Profile page, however within the profile page there is a area which shows weather they're online, or offline.

 

At the moment, it's just text showing their name and it just says online. We're using cookies to register logins, as well as sessions to use information like username/password, ect ect.

 

If anyone could give us a hand on how we can make it show weather they're online or offline work, I'd appreciate it.

 

Thanks. Here's a preview on what it looks like, just to give you an idea.

 

5779da2cd9254aecb9db50e.png

 

See how I'm not logged in, yet it says online? Thank you. :)

Link to comment
https://forums.phpfreaks.com/topic/270247-check-if-user-is-logged-on-or-offline/
Share on other sites

Like Jessica said, you will have to keep track of everything they do. Simply storing the timestamp of when they logged in will not cut it. In a forum like this, every thing is registered and logged.. simply clicking this reply box counts as me being online. What you will have to do in the long run is restructure your current system to log everything and anything... the more you log the more accurate your online/offline status will be.

 

 

And no, I am not suggesting that you create a table for all of these logs. You simply need two extra columns in your table. One to describe the action and the other to hold the timestamp... or DATETIME format which would be a lot more efficient.

Edited by Zane

Let's say you have a link somewhere for say... reports. In the href of this link, you would add a flag variable to it like

Reports

Then on your reports.php page, you would check for that flag, create a timestamp if it is there and then update the database. You could roll all of that up into a single function

 

Of course, you can only run this function if a user is logged in. So do that check first.

 

When they log out, you can update that column to NULL.

 

All you are doing is taking the exact time of when they click a link or something else and putting that time into the database. You would also put this time into the SESSION variable for quicker access.

 

Once you have that, you find your existing queries and while loops for getting the info about your users. If the time in the activity column is older than 15, 20, 30 ..whatever minutes you feel like using, then they are offline.

 

You can have your SQL return you a boolean for online.

 

SELECT *, CASE  
WHEN TIMESTAMPDIFF(MINUTE , activity, NOW()) > 15
THEN 0 
ELSE 1
END AS isOnline

 

Then, in your PHP script you can use a simple ternary statement to display Online or Offline

 

$onlineStatus = $row['isOnline'] ? "Online" : "Offline"

Yeah the "flag" isn't really necessary. I just put that there to explain how to record a description of the activity. Like in IPB when you post a reply it grabs a certain script and appends something like do=postreply which is then sent to a switch I am assuming and an activity description is created.... $username . "is posting a reply in " . $topic..

 

But yea... the flag isn't necessary if you aren't recording such things.

Exactly. after so many minutes you can consider a user inactive and at that point you can do a number of things.

You could

- destroy their session ... logging them out completely

- keep their session allowing them to stay online but appear offline

- create some sort of popup telling the user they have been logged out for inactivity like most bank portals do

- well, I suppose that is about the most you could do...without redirecting them to google or something.

Zane but if i grant powers to a user based on the fact that he is loged in that means i know how to check the use access with what ever i use to check be it session and other.

 

For simple application where i only want to show user online or offline massage why dont i just use the same sessionif (isset($_SESSION[])) that ggrants access to set a user loged in valuable that i can use to show user loged.

I cannot understand your response. Are you trying to say that simply checking the Session variable is sufficient to determine logged in or logged out? I am lost in what you are trying to tell me..it is most definitely a language barrier problem.

Yes, but only if you destroy the session after 15 minutes of inactivity. Otherwise, users would be declared as online/active until they click logout or they close the browser, then you would have to make sure any cookies are destroyed as well therefore they could be assumed online/active for quite some time... depending on the expiry settings you have for your sessions and cookies.

 

Storing it in the database would allow you to create a list of who is online/active. Without the use of a database, you would have to populate some external file on every page load with a list of who is online or offline..

Edited by Zane

Hi, i read what you said but i cannot seem to figure it out, you did not explain much of how to do it or what functions to use.

 

i am using this

 

<?
session_start();
include 'config.php';
$time = time() + (0 * 0 * 15 * 0);
$username = $_SESSION['username'];
mysql_query("UPDATE `dragonx_sql`.`users` SET timestamp=$time WHERE username='$username'") or die(mysql_error());
?>

 

but i dont know if that is correct, if not what is, and if it is correct how can i get 15 minutes from this....

timestamp fields can be set to automaticly update when a change is made to the record that they are a part of. My preffered method is actualy have 2 timestamp fields. 1 for last action, and another for previous action (it's probably just me being a data junky). I then set last action to auto update to current time stamp whenever the record is updated, and make my query to update the table by selecting last action and updating it into previous action. This also gives the bonus, from an analisys point of view, that you can start to build an "average time between actions" to make sure that your auto logouts are set to a time frame that suits the site it's self - assuming your into that kind of thing...

Here's a snip that I found lying about, it's a function that uses one of my custom db classes, but the SQL is still there.

function updateLog($uid){
$con = new MyCon("intra");
$qry = "UPDATE com_log SET prevLogin = lastLogin WHERE uID = $uid";
$uDate = $con->qry($qry);
$reply = $con->message;
return $reply;
}

you should be able to easily replace the custom class code with whatever you use.

Edited by Muddy_Funster

still not what i asked for :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused:

 

i need to know what function records the timestamp, and what checks for the difference

 

like i need a function to record the stamp then i also need one to check the difference....

You may also apply the same concept explained by Jessica when user login insert a time from time(); function in the database.

 

And when you check for user login or not put this code of script.

 

$inactive = 300; // for 5 minute

$current_time = time();

$prev_time = /*select prev time from the database*/

 

$session_life = $current_time - $prev_time;

if($session_life > $inactive)

{

unset($_SESSION[$user]);

}

else

{

$sql="update $table set $field_name = $current_time";

 

}

 

i hope this will be helpfull for you

Edited by Anandkp

still not what i asked for :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused:

 

i need to know what function records the timestamp, and what checks for the difference

 

like i need a function to record the stamp then i also need one to check the difference....

 

We are not here to write code for people. You have been provided many answers to your question. Start writing some code and get back to use when you are stuck with a particular issue.

Guest
This topic is now 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.