PHP who is online

Ask about a PHP problem here.
Post Reply
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

PHP who is online

Post by irfanh94 »

How can i echo the users who online on my site (who is logged in)..
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP who is online

Post 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.
Image
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP who is online

Post by irfanh94 »

Ok, and how i can echo time(); to the real time, example: 19:34
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP who is online

Post by jacek »

date() is used to print a formatted time, is that what you need ?
Image
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP who is online

Post 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"?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP who is online

Post by jacek »

That's the basics of it yes. :D
Image
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP who is online

Post by irfanh94 »

Huh, i understand it now. Im going to code it, and post it here again, to see is it correct.
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP who is online

Post 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'];
		}
	}

?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP who is online

Post 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.
Image
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP who is online

Post by irfanh94 »

Am i going to use timestamps on insert query?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP who is online

Post 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.
Image
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP who is online

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP who is online

Post by jacek »

post your code ;)
Image
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP who is online

Post 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'];
							}
						}
					?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP who is online

Post 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 :)
Image
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP who is online

Post by irfanh94 »

Thanks good, and also thanks to you. I fixed it.
Post Reply