Jump to content


Photo

[solved] online now?


  • Please log in to reply
24 replies to this topic

#1 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 03:11 AM

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!

#2 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 26 August 2006 - 04:30 AM

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..

<?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";
?>

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...

#3 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 05:01 AM

i dont want numbers i want names... you know like on myspace? when a user log's in an image changes...

#4 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 26 August 2006 - 05:15 AM

ahh you want a list of names for those who are logged in.. okay how bout this 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'].', ';
}
?>

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, "




#5 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 05:22 AM

how would i insert that? can u give me more details on the table layout?

#6 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 26 August 2006 - 05:25 AM

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.

#7 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 05:28 AM

ok cool... i will try...

#8 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 05:36 AM

can u tell me what '$time = time() - 60 * 15;' does?

#9 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 26 August 2006 - 05:42 AM

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...

#10 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 05:45 AM

oh... because its not working... i used
<?php $time2 = time(); echo "$time2"; ?>
and got the time... and put that into the table...in all rows and it did not display anything...

#11 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 26 August 2006 - 05:47 AM

do it like this..

<?php
$time2 = time();
echo $time2;
?>


#12 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 05:49 AM

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???  ???

#13 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 26 August 2006 - 05:51 AM

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...

#14 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 05:56 AM

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]

#15 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 26 August 2006 - 05:59 AM

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....

#16 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 06:01 AM

i want online and offline... sorry if i was not clear... oops...

#17 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 26 August 2006 - 06:13 AM

yeah this is gonna be easier.. much much much easier.. lol

First 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
<?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">';
}
?>

This should be more at what you are looking for...  The code before was trying to list all of their usernames.. lol...

#18 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 06:18 AM

yeah but that is just looking up the thing and its not going to expire (like with the time)

#19 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 26 August 2006 - 06:19 AM

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...

#20 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 26 August 2006 - 06:24 AM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users