By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

Know the users online ?

100+
P: 150
i need to know the users on line or off line, that is my code:

[PHP]
$time = time();
$timech = time()-7;
$user = $_SERVER['REMOTE_ADDR'];
$session = $_SESSION['chat'];

$host = "localhost";
$username = "root";
$password = "";
$dbname = "chat";

$connect = mysql_connect("$host", "$username", "$password");
mysql_select_db("$dbname");
$online = mysql_query("SELECT * FROM online");
while($row = mysql_fetch_array($online)){
if($row['session']==$session){
$uexi = true;
}
}
if($uexi == true){mysql_query("UPDATE online SET time='$time',user='$user' WHERE session = '$ses'");}else{mysql_query("INSERT INTO online (session, time, user) VALUES ('$ses', '$time' ,'$user')");}

mysql_query("DELETE FROM online WHERE time<$timech");
[/PHP]

i used this code in my little chat room but i want to use another technique because i think that code will effect in the site simplicity, in my chat room I'm using Ajax to check every second if the user on line or not, i mean that this code every one will enter the site will check if any one if his session time expired or not this code will take long time to users having slow connection.
May 21 '08 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
Ok, first of all:
Expand|Select|Wrap|Line Numbers
  1. $online = mysql_query("SELECT * FROM online");
  2. while($row = mysql_fetch_array($online)){
  3.   if($row['session']==$session){
  4.      $uexi = true;
  5.   }
  6. }
  7.  
You are fetching the entire table to check a single row.
This is about the most resource-wasting thing you can do!

Try using the WHERE clause to have MySQL search for the row instead. It will reduce the time and resources needed for this by a LOT
Expand|Select|Wrap|Line Numbers
  1. $online = mysql_query("SELECT * FROM online WHERE session = {$session}");
  2. if(mysql_num_rows($online) > 0) {
  3.   $uexi = true;
  4. }
  5.  
May 21 '08 #2

TheServant
Expert 100+
P: 1,168
... in my chat room I'm using Ajax to check every second if the user on line or not, i mean that this code every one will enter the site will check if any one if his session time expired or not this code will take long time to users having slow connection.
What about checking less often? Every second connecting to the database could be somewhat resource intensive, inparticular if you have 100 users chatting.100 connections a second pulling whole rows?! Might I also recommend instead of SELECT *, choosing the variables you need (assuming you don't need them all):
[PHP]SELECT user_name, user_time, user_age FROM table1 WHERE online="1"[/PHP]

Just a thought. Oh and what is
$time = time();
$timech = time()-7;
for? If you are doing this for all your pages, why don't you offset your server time? You need to change you htaccess file.
May 21 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.