JP128 Posted December 19, 2006 Share Posted December 19, 2006 What would I need to do to have something showing that a users is currently online? I am sure that a column is needed with a set value, but I am not sure on the other things needed. Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 19, 2006 Share Posted December 19, 2006 I am sure there are several variations on this and ways to do it. Are you purely wanting to show if a specific user is online or the current number of users online? Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 19, 2006 Author Share Posted December 19, 2006 specific.. Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 19, 2006 Share Posted December 19, 2006 First thing to do is add a column to your users table to be used to record whether a user is online or not. There a couple of variations this can be used, either to record purely either 'online' or 'offline' or to record a timestamp when a user logs in or is active. The problem with using 'online'/'offline' it is going to be difficult to update when a user is not online so I would recommend using a timestamp column.#ALTER TABLE tablename ADD COLUMN columnname DATETIME;Thats the easy bit! The train of thought here is that whenever a user logs in or access another page, this column should be updated with a current timestamp. Then when wanting to see if a user is online, you can query this column and check to see if the timestamp is within a set amount of time from the current time. If within the preset time limit, we assume the user is online. You will have to decide on the time limit you are going to check within, but obviously this is not going to be 100% accurate because a user may log out at anytime within the time limit.Am I making sense this far? Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 19, 2006 Author Share Posted December 19, 2006 yes Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 19, 2006 Share Posted December 19, 2006 You need to add a bit of script on the beginning of each page in your site that will update your new column. I suggest you do this in an include file. You will need to check if there is a user logged in first, and then run a query to update the timestamp.How do you record users are logged in? Do you use sessions? What variables do you use in the session? Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 19, 2006 Author Share Posted December 19, 2006 Sessions, and right now, I have a way to do it.. when the log on, set useronline to 1, and if its 1, say they are online, and when they click logout.. set to 0. $_SESSION['username']... but I need to go to sleep.add me to AIM: Stropfo, or yahoo messenger: johnny_ensign@yahoo.com Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 19, 2006 Share Posted December 19, 2006 I use MSN messenger, do you?The trouble with the method you are detailed is that if a user does not logout, the value will remain as 1 and will show a user as online even when they may not be. Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 19, 2006 Author Share Posted December 19, 2006 Whats your MSN? Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 19, 2006 Share Posted December 19, 2006 I have added you to my contact list Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 19, 2006 Author Share Posted December 19, 2006 did you add johnny_ensign1545@msn.com? thanks...And if anyone else knows a way to submit a mysql query if a user X\'es out of the page that would be great... Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 19, 2006 Share Posted December 19, 2006 the easiest way would be to add a field to your user table called last_onlinethen create a lil function that runs at the load of every page that updates that fieldthen run a query to see who has been online in the last x mins Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 19, 2006 Author Share Posted December 19, 2006 what command would I use for that? I know I could use the timestamp, which would give me a time like 2006-12-18 15:05:12... but then what do i do to check to see if the user was on 300 seconds ago(5 mins) Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 19, 2006 Share Posted December 19, 2006 [quote author=emehrkay link=topic=119225.msg488299#msg488299 date=1166547623]the easiest way would be to add a field to your user table called last_onlinethen create a lil function that runs at the load of every page that updates that fieldthen run a query to see who has been online in the last x mins [/quote]Thats what I said to start with? Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 20, 2006 Author Share Posted December 20, 2006 Lol, Yes, chip, it was. But now I am asking how that would be accomplished... Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 OK, couple of assumptions then: you have table 'users' to which you have added the column 'online' (DATETIME) and when a user is logged in, you have a session variable called 'username' which is only present when somebody is logged in. The table 'users' also has column 'username'.Create the following file and save as online.php[code]<?phpif (isset($_SESSION['username'])) { // checking to see if there is a user logged in// user logged in so update current time in 'online' column of 'user' table$query = "UPDATE users SET online = now() WHERE username = \"".$_SESSION['username']."\"";$result = mysql_query($query);}?>[/code]Now include the above script at the beginning of every page, ensuring that you position it after you start the session and connect to the database[code]<?php// start sessionsession_start();// connect to database// include new scriptrequire('online.php');?>[/code]Now all you have to do is run a query to see if the timestamp stored in the online column is within a tolerable recent time limit to see if the user is online. We are going to use 5 minutes as our tolerable time limit.[code]<?php// create query that will return the difference between current time and 'online' time// this will be returned in seconds but for some reason in this calculation the figure is based// on 100 seconds in a minute!?$query = "SELECT now() - online AS diff FROM users WHERE username = \"".$_SESSION['username']."\"";$result = mysql_query($query);$row = mysql_fetch_array($result);// check returned value is within toleranceif($row['diff'] < 500) {// user has been online within the last 5 minutesecho "User is online";} else {// user has NOT been online within the last 5 minutesecho "User is offline";}?>[/code]I have not checked this last bit but give it all a go and see if it works! Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 20, 2006 Author Share Posted December 20, 2006 hey, what is your msn btw? Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 20, 2006 Author Share Posted December 20, 2006 I modded your script some Chip... I made the include this: [code]<?php$query1 = "SELECT username,online FROM users WHERE now() - online<=500";$result1 = mysql_query($query1);while ($row1 = mysql_fetch_assoc($result1)){if($row1['diff'] < 500) {// user has been online within the last 5 minutesecho "<a href='http://jp128.mooo.com/userprofile.php?username=".$row1['username']."'>".$row1['username']."</a> ";}}?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.