Know the users online ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smartic
    New Member
    • May 2007
    • 150

    Know the users online ?

    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("SE LECT * FROM online");
    while($row = mysql_fetch_arr ay($online)){
    if($row['session']==$session){
    $uexi = true;
    }
    }
    if($uexi == true){mysql_que ry("UPDATE online SET time='$time',us er='$user' WHERE session = '$ses'");}else{ mysql_query("IN SERT INTO online (session, time, user) VALUES ('$ses', '$time' ,'$user')");}

    mysql_query("DE LETE 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.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Ok, first of all:
    [code=php]
    $online = mysql_query("SE LECT * FROM online");
    while($row = mysql_fetch_arr ay($online)){
    if($row['session']==$session){
    $uexi = true;
    }
    }
    [/code]
    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
    [code=php]
    $online = mysql_query("SE LECT * FROM online WHERE session = {$session}");
    if(mysql_num_ro ws($online) > 0) {
    $uexi = true;
    }
    [/code]

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Originally posted by smartic
      ... 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.

      Comment

      Working...