People online list edit.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
DeathsMessenger
Forum Newbie
Posts: 20
Joined: Wed Jan 17, 2007 4:08 am
Location: England

People online list edit.

Post by DeathsMessenger »

well i figured that the online list that i coded was flawed because it only shows how many people are online. not who is online. And i am trying to think of a way to make it like that.

Here is the counter

Code: Select all

<?php  
if (! isset($_SESSION['online'])) {   
@mysql_query("INSERT INTO ppl_online (session_id,activity,ip_address,refurl,user_agent) VALUES ('" .session_id(). "', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGENT']}')");   
$_SESSION['online'] = $online;
} else {   
if (isset($_SESSION['first_name'])) {   
@mysql_query("UPDATE ppl_online SET activity=now(),member='y' WHERE session_id='" .session_id(). "'");   
} 
 }
 if (isset($_SESSION['online'])) {         
    @mysql_query("UPDATE ppl_online SET activity=now() WHERE session_id='" .session_id(). "'");   
} 
  if (date('H') == 00) { // 00-23 hours. or 'D' == 'Sun'  
          @mysql_query("TRUNCATE TABLE ppl_online"); // Deletes all records. Faster than DELETE.   
   }
 ?>
And the script displays the results

Code: Select all

<?   
$limit_time = time() - 300; // 5 Minute time out. 60 * 5 = 300   
 $sql = mysql_query("SELECT * FROM ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='n' GROUP BY ip_address") or die (mysql_error());   
$sql_member = mysql_query("SELECT * FROM ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='y' GROUP BY ip_address") or die (mysql_error());   
 $visits = mysql_num_rows($sql);   
$members = mysql_num_rows($sql_member);   
echo "Guests Online: $visits<br/>";  
echo "Members Online: $members<br/>";   
?>

which usually outputs
Guests Online: 7
Members Online: 2

how should i go about changing the out put to

Guests Online: 7
Members Online: DM, Javisen
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

First, you should never use "select *" and mysql_num_rows() just to get the number of rows. You should use "select count(*)" instead.

Second, I can't see where you put the member names in your DB - maybe you have it in another table ( where do you get $_SESSION['first_name'] from?).
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
DeathsMessenger
Forum Newbie
Posts: 20
Joined: Wed Jan 17, 2007 4:08 am
Location: England

Post by DeathsMessenger »

I didn't know about that one thank you. but after reading some of the other topics in the forum i found that this would be way simpler.

Code: Select all

<?php
mysql_query("UPDATE users SET online=1 WHERE id='$user->id'");
?>
...In the header.

Code: Select all

<?php
mysql_query("UPDATE users SET online=0");
?>
...On a 5 minute cron.

But i still cannot figure out how to make a query find all the accounts with only the online set to 1.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

DeathsMessenger wrote:I didn't know about that one thank you. but after reading some of the other topics in the forum i found that this would be way simpler.
It uses too much resourses which u don't use later. Another advice: try not to use "select *" when you dont need all of the fileds from a table - use select col1, col2,.... instead.
DeathsMessenger wrote:But i still cannot figure out how to make a query find all the accounts with only the online set to 1.
Still waiting you to answer my second question :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

DeathsMessenger wrote:But i still cannot figure out how to make a query find all the accounts with only the online set to 1.
You'd want a WHERE clause that checked the value of `online`. It's fairly simple.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Right now you have all the information you need to get what you want (based on your original post). Just read the query result into an array using something like mysql_fetch_array() then use the resultant array to list who is online.
User avatar
DeathsMessenger
Forum Newbie
Posts: 20
Joined: Wed Jan 17, 2007 4:08 am
Location: England

Post by DeathsMessenger »

Thanks guys you solved my problem.

VladSun wrote:
DeathsMessenger wrote:But i still cannot figure out how to make a query find all the accounts with only the online set to 1.
Still waiting you to answer my second question :)
Oh it was a cookie that is set login, i took it off a while ago though.[/quote]
Post Reply