Page 1 of 1
PHP who is online
Posted: Sun May 15, 2011 4:49 pm
by irfanh94
How can i echo the users who online on my site (who is logged in)..
Re: PHP who is online
Posted: Sun May 15, 2011 5:06 pm
by jacek
You would have to do something like store the last active time in a table, update it every page load. And to get the active users do a query to get the users where this time is within 5 minutes.
Re: PHP who is online
Posted: Sun May 15, 2011 5:36 pm
by irfanh94
Ok, and how i can echo time(); to the real time, example: 19:34
Re: PHP who is online
Posted: Sun May 15, 2011 6:05 pm
by jacek
date() is used to print a formatted time, is that what you need ?
Re: PHP who is online
Posted: Sun May 15, 2011 6:47 pm
by irfanh94
Look, i think to do design of table like this:
session name, ip adress, lastlogin (including refreshing the page). I after this i think i should do this:
$timelogin = actual time - lastlogin
and if $timelogin > 15 minutes echo nothing, else get from the database all users who logged in for 15 minutes.
Is this correct? Is this the way i should do the "who is logged in"?
Re: PHP who is online
Posted: Sun May 15, 2011 6:49 pm
by jacek
That's the basics of it yes.
Re: PHP who is online
Posted: Sun May 15, 2011 6:54 pm
by irfanh94
Huh, i understand it now. Im going to code it, and post it here again, to see is it correct.
Re: PHP who is online
Posted: Sun May 15, 2011 6:59 pm
by irfanh94
Ok, here is the code:
<?php
$actualtime = date("h:m");
$query = mysql_query("SELECT * FROM `sf_sessions`");
while ($row = mysql_fetch_assoc($query)) {
$pastime = $row['lastlogin'];
$timeloggedin = $actualtime - $pastime;
if ($timeloggedin >= 15) {
echo 'Users logged in for 15 minutes:' $row['sessioname'];
}
}
?>
Re: PHP who is online
Posted: Sun May 15, 2011 7:09 pm
by jacek
Hmm, you will need to use a timestamp to do the math.
what id the format of the lastlogin column ?
you can probably do it with just one query
SELECT `sessionmame` FROM `sf_sessions` WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`lastlogin`) < 900
something like that anyway.
Re: PHP who is online
Posted: Sun May 15, 2011 7:11 pm
by irfanh94
Am i going to use timestamps on insert query?
Re: PHP who is online
Posted: Sun May 15, 2011 7:19 pm
by jacek
irfanh94 wrote:Am i going to use timestamps on insert query?
That's up to you
It would certainly make the maths easier.
Re: PHP who is online
Posted: Sun May 15, 2011 7:26 pm
by irfanh94
God damn, here is the new problem.
When i put the date like this: date("h:m"); it always repeat same time 09:05..
EDIT: again new problem:
look on the right side of
http://brainiac-design.info/probe/sfilms/, i just want to echo one time user, not to repeat.
Re: PHP who is online
Posted: Sun May 15, 2011 7:57 pm
by jacek
post your code
Re: PHP who is online
Posted: Sun May 15, 2011 7:58 pm
by irfanh94
Imoporting the users:
$member = $_SESSION['member'];
if ($member) {
$time = time();
$ipadress = $_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `sf_sessions` (sessionname, ipadress, lastlogin) VALUES ('$member','$ipadress','$time')");
echo mysql_error();
}
Echoing the users:
<?php
$currentime = time();
$query = mysql_query("SELECT `sessionname` FROM `sf_sessions` WHERE '$currenttime' - `lastlogin` < 900 ORDER by lastlogin desc");
echo mysql_error();
if ($query) {
while ($row = mysql_fetch_assoc($query)) {
echo $row['sessionname'];
}
}
?>
Re: PHP who is online
Posted: Sun May 15, 2011 8:09 pm
by jacek
Are you inserting that row on every page load ? because it should be an UPDATE.
the "ON DUPLICATE KEY UPDATE" syntax may be of use
Re: PHP who is online
Posted: Sun May 15, 2011 8:21 pm
by irfanh94
Thanks good, and also thanks to you. I fixed it.