Page 1 of 1

People online list edit.

Posted: Thu Aug 02, 2007 11:36 am
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

Posted: Thu Aug 02, 2007 11:58 am
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?).

Posted: Thu Aug 02, 2007 2:40 pm
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.

Posted: Thu Aug 02, 2007 3:13 pm
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 :)

Posted: Thu Aug 02, 2007 5:01 pm
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.

Posted: Thu Aug 02, 2007 6:40 pm
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.

Posted: Fri Aug 03, 2007 3:25 am
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]