pro_se Posted August 26, 2006 Share Posted August 26, 2006 hiya! i am working on a web community and the last project i have to tackle is the online now image. i was wondering if anyone in this genius bucket knows how to create a mysql/ php session online now thing. okay now some backend information -- the session is set when a user logs in, this session is called $_SESSION['username'];. also, when a user logs in a session variable gets set to 1 (meaning logged in) this variable is called $logged_in... hope thats enough information and all your input is greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/ Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 I've done this before.. #1 thing you need to have a time stamp saved for every user logged in.. I generally do this in the members table in a field called last_activity#2 if You want to calculate how many guests are online -- you might want to make another table called "Online_Guests" with 1 field with the name of "Username"#3 -- how long do you want it delayed? and do you want to be able to change it? If you want to change it then you'd be better off doing a database setup on that part and put say 15 in the delay for 15 minutes.. Once you have that -- this would be your basic code..[CODE]<?php$time = time() - 60 * 15;$mems = "SELECT * FROM members WHERE last_activity <= $time";$mems = mysql_num_rows(mysql_query($mems), 0);$guest = "SELECT * FROM online_guests";$guest = mysql_num_rows(mysql_query($guest), 0);$tot = "SELECT * FROM members";$tot = mysql_num_rows(mysql_query($tot), 0);echo $mems." members online";echo $guest." guests online";echo $tot." total members";?>[/CODE]Keep in mind i just threw that together -- it may have a few bugs but for the most part it could work.. you could also instead of using the mysql_num_rows command use the count(*) in the select query instead of just *.. Hope it has helped.. have fun... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80560 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 i dont want numbers i want names... you know like on myspace? when a user log's in an image changes... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80563 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 ahh you want a list of names for those who are logged in.. okay how bout this code...[CODE]<?php$time = time() - 60 * 15;$mems = "SELECT * FROM members WHERE last_activity <= $time";$mems = mysql_query($mems);$cnt = mysql_num_rows($mems);for ($i = 0; $i < $cnt && $rows = mysql_fetch_assoc($mems); $i++) {echo $rows['username'].', ';}?>[/CODE]Something similar to this may be what you are looking for... Now also keep in mind I have not tested this and it may not work as planned but it should get you on your way to what you want...Breakdown of the code:Select * from database where last activity is 15 minutes ago or after..$cnt - count how many entries are in the results...for loop -- i = 0 and is less than $cnt lets get an associative array and up it to the next info...echo $rows['username'].', ' will simply print out "Radar, " Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80568 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 how would i insert that? can u give me more details on the table layout? Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80570 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 Well you should already have a table that you have all your members listed in.. just add a field at the end called last_activity -- and then on every page have it update their last activity by using the mysql command UPDATE..Then to get the time for the update just use $time = time(); this will give you a series of digits in seconds... seconds are easier to work with...Once that is done, you impliment the code above wherever you want it to show on your site. Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80572 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 ok cool... i will try... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80573 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 can u tell me what [b]'$time = time() - 60 * 15;'[/b] does? Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80575 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 time() gets you the ammount in seconds for the current date and time... - 60 means minus 60 seconds.. and * 15 means 60 * 15 which is 15 minutes.. so basically $time = time() - 60 * 15; just makes the time it is now minus 15 minutes... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80577 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 oh... because its not working... i used [code]<?php $time2 = time(); echo "$time2"; ?>[/code] and got the time... and put that into the table...in all rows and it did not display anything... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80578 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 do it like this..[CODE]<?php$time2 = time();echo $time2;?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80579 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 ok and i put that into all the rows in the table and it did not display any of the people... and im sure it has not been 15 min's yet??? ??? Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80580 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 Let me see your page that your running.. And if you can -- attach the file for the page you're working on... and i'll take a look at it too... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80581 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 its on a local server so i will attach it... the sql dump: CREATE TABLE `users` ( `status` varchar(255) NOT NULL default 'working', `rel` varchar(255) NOT NULL default '', `orient` varchar(255) NOT NULL default '', `gend` varchar(255) NOT NULL default '', `id` int(10) NOT NULL auto_increment, `name` varchar(20) NOT NULL default '', `username` varchar(40) default NULL, `password` varchar(50) default NULL, `regdate` varchar(20) default NULL, `email` varchar(100) default NULL, `website` varchar(150) default NULL, `location` varchar(150) default NULL, `last_login` varchar(20) default NULL, `fronttext` text NOT NULL, `headline` varchar(50) NOT NULL default 'This is my Headline', `counter` varchar(200) NOT NULL default '1', `avatar` varchar(60) NOT NULL default 'images/user/nopic.gif', `hobbies` varchar(255) NOT NULL default '', `music` varchar(255) NOT NULL default '', `reading` varchar(255) NOT NULL default '', `heroes` varchar(255) NOT NULL default '', `mstatus` varchar(255) NOT NULL default '', `inout` varchar(50) NOT NULL default 'out', `fname` varchar(255) NOT NULL default '', `lname` varchar(255) NOT NULL default '', `showdetails` char(1) NOT NULL default 'n', `showgeneral` char(1) NOT NULL default 'n', `last_activity` varchar(255) NOT NULL default '', PRIMARY KEY (`id`)) [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80582 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 Okay now i have a question.. Do you want it so that when they log in there is an "online" or "offline" image or are you trying to list their usernames? maybe ive given you the wrong code.... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80583 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 i want online and offline... sorry if i was not clear... oops... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80584 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 yeah this is gonna be easier.. much much much easier.. lolFirst off -- you'll need to be able to have a way to read the user of the profile or whatever.. some sort of a string that the username the online/offline image is going to show up under... in the instance of my code imma use $user_name[CODE]<?php$time = time() - 15 * 60$mem = "SELECT inout FROM users WHERE username = '$user_name'";$mem = mysql_query($mem);$mem = mysql_result($mem, 0);if ($mem == "in") {echo '<img src="in.gif">';} else {echo '<img src="out.gif">';}?>[/CODE]This should be more at what you are looking for... The code before was trying to list all of their usernames.. lol... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80585 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 yeah but that is just looking up the thing and its not going to expire (like with the time) Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80587 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 i dont think i am looking for how to display it... i am looking for how to insert it and how it will run out with time... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80588 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 okay so lets do it like this instead...<?php$time = time() - 15 * 60$mem = "SELECT last_activity FROM users WHERE username = '$user_name'";$mem = mysql_query($mem);$mem = mysql_result($mem, 0);if ($time => $mem) {echo '<img src="in.gif">';} else {echo '<img src="out.gif">';}?>Then as said before you would use the UPDATE query command to update the last_activity with $time = time() ... this seem more like what you want? Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80590 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 Parse error: parse error, unexpected T_DOUBLE_ARROW in /var/www/dev.php on line 3232: if ($time => $mem) { Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80598 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 try swapping the order >= that might be the way... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80599 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 10 in /var/www/dev.php on line 3131: $mem = mysql_result($mem, 0); Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80600 Share on other sites More sharing options...
radar Posted August 26, 2006 Share Posted August 26, 2006 Okay try changing the query I gave you to this (both lines of $mem other than the result)..[CODE]$mem = "SELECT last_activity FROM users WHERE username = '$user_name'";$mem = mysql_query($mem) or die("Error: ". mysql_error(). " with query ". $mem); // what's wrong?[/CODE]change it to that -- run it again and then tell me what it says... Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80607 Share on other sites More sharing options...
pro_se Posted August 26, 2006 Author Share Posted August 26, 2006 hey maan! i did some serious deep meditation... lol... i used the first code u gave me and modified to with an if and else and changed the time diff to 5 min and $username = $_SESSION... and guess what! it worked!!!!!! solved. Link to comment https://forums.phpfreaks.com/topic/18685-solved-online-now/#findComment-80630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.