deathadder Posted November 3, 2012 Share Posted November 3, 2012 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. See how I'm not logged in, yet it says online? Thank you. Link to comment Share on other sites More sharing options...
Jessica Posted November 3, 2012 Share Posted November 3, 2012 Have a field in the users table that stores the time stamp of their last activity. Update it every page load. If its > 5,10, whatever minutes old ... They're offline. Link to comment Share on other sites More sharing options...
hell_yeah Posted November 3, 2012 Share Posted November 3, 2012 You may also apply the same concept explained by Jessica but having a separate table for user online, just an opinion Link to comment Share on other sites More sharing options...
Jessica Posted November 3, 2012 Share Posted November 3, 2012 Why would you do that? Link to comment Share on other sites More sharing options...
Zane Posted November 3, 2012 Share Posted November 3, 2012 (edited) 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 November 3, 2012 by Zane Link to comment Share on other sites More sharing options...
deathadder Posted November 4, 2012 Author Share Posted November 4, 2012 Hi, i have never worked with timestamps before, and i don't exactly understand how i am going to log actions, if you could post a bit of a snippit or explain a bit more or what to do. Link to comment Share on other sites More sharing options...
Zane Posted November 4, 2012 Share Posted November 4, 2012 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" Link to comment Share on other sites More sharing options...
deathadder Posted November 4, 2012 Author Share Posted November 4, 2012 Wow everyone, thank you for the contribution. I will have a go and find out how to use the timestamp(). Thanks. Link to comment Share on other sites More sharing options...
Jessica Posted November 4, 2012 Share Posted November 4, 2012 .... What? Why would you add something to the URL? Look, on every page you should be checking if they're logged in anyway. Just add an update query right after that. Link to comment Share on other sites More sharing options...
Zane Posted November 4, 2012 Share Posted November 4, 2012 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. Link to comment Share on other sites More sharing options...
50r Posted November 4, 2012 Share Posted November 4, 2012 What happens whe a user logs in, and after that he doesn't do any thing (Innactive but loged in)? will that mean that the system will report the user as not loged in because he is inactive? Link to comment Share on other sites More sharing options...
Zane Posted November 4, 2012 Share Posted November 4, 2012 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. Link to comment Share on other sites More sharing options...
50r Posted November 4, 2012 Share Posted November 4, 2012 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. Link to comment Share on other sites More sharing options...
Zane Posted November 4, 2012 Share Posted November 4, 2012 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. Link to comment Share on other sites More sharing options...
50r Posted November 6, 2012 Share Posted November 6, 2012 i was asking that cant it work if i only used the session that i set on login until its destroyed. then i can know that the user is no mre logged in? Link to comment Share on other sites More sharing options...
Zane Posted November 6, 2012 Share Posted November 6, 2012 (edited) 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 November 6, 2012 by Zane Link to comment Share on other sites More sharing options...
50r Posted November 6, 2012 Share Posted November 6, 2012 now that you talked of the database i get the idea. Link to comment Share on other sites More sharing options...
deathadder Posted November 8, 2012 Author Share Posted November 8, 2012 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.... Link to comment Share on other sites More sharing options...
Muddy_Funster Posted November 8, 2012 Share Posted November 8, 2012 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... Link to comment Share on other sites More sharing options...
deathadder Posted November 8, 2012 Author Share Posted November 8, 2012 yes, thanks but this did not answer my question, could you possibley post a snippit of the code/functions used Link to comment Share on other sites More sharing options...
Muddy_Funster Posted November 8, 2012 Share Posted November 8, 2012 (edited) 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 November 8, 2012 by Muddy_Funster Link to comment Share on other sites More sharing options...
deathadder Posted November 9, 2012 Author Share Posted November 9, 2012 still not what i asked for :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.... Link to comment Share on other sites More sharing options...
Anandkp Posted November 9, 2012 Share Posted November 9, 2012 (edited) 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 November 9, 2012 by Anandkp Link to comment Share on other sites More sharing options...
trq Posted November 9, 2012 Share Posted November 9, 2012 still not what i asked for :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. Link to comment Share on other sites More sharing options...
Recommended Posts