Page 1 of 1

Users Online

Posted: Wed Jul 20, 2011 10:29 am
by ConceptDestiny
Users online code:
Difficulty: beginner

Table: "user_online"
Columns:
- session (VARCHAR 45)
- time (INT 30)
- username (VARCHAR 30)

Code: Select all

          
// Check to see if user is signed in. the below code portion can go in your header.php, which is included on every page.
          if (isset($session_username))
          {     
                  // set session variables, time calc, etc.
                  $session=session_id();
                  $time=time();
                  $time_check=$time-600;
                 
                  // The query below checks to see if the player's session already exists         
                  $sql="SELECT * FROM user_online WHERE session='$session'";
                  $result=mysql_query($sql);   
                  $count=mysql_num_rows($result);
                 
                      if($count=="0")
                      {
                          // if $count == 0 means there are no results back from the above query, so its fine to insert a new session for the player                        
                          $sql1="INSERT INTO user_online(session, time, user)VALUES('$session','$time','$username')";
                          $result1=mysql_query($sql1);
                      }
                      else
                      {
                          // if $count did not = 0, and instead there was a result, this means the session already exists in the database table "user_online", so instead of inserting a new session, we update its time!
                          "$sql2=UPDATE user_online SET time='$time' WHERE session='$session'";
                          $result2=mysql_query($sql2);
                      }
     
                   // The portion below checks to see if the session is old/inactive, if this is true, the session is deleted from the user_online table
                    $sql3="SELECT * FROM user_online";
                    $result3=mysql_query($sql3);
                    $count_user_online=mysql_num_rows($result3);   
                    // if over 10 minute, delete session   
                    $sql4="DELETE FROM user_online WHERE time<$time_check";
                    $result4=mysql_query($sql4);
          }

Code: Select all

          
                // the portion below lists those players that are in the user_online table
                    
                $sqlonline="SELECT user FROM user_online";
                $resultonline=mysql_query($sqlonline);
                $count_user_online=mysql_num_rows($resultonline);
                  if ( $count_user_online == 0) 
                    {
                        $count_user_online = '0';
                    }
                    
                  echo "<center><table width='800' bgcolor='#090401'><tr><td><center>";
                  
                    {
                      echo "<small>There are $count_user_online users online<br><br><b>Users Online:</b><br></small>";
                    }
                  if ( $count_user_online > 1)
                    {
                      $comma = ",";
                    }
                  if ( $count_user_online > 0)
                    {
                       while($rowonline = mysql_fetch_array($resultonline))
                            {
                                echo "".$rowonline['user']."$comma ";
                            }
                    }
                    
                    echo "</td></tr></table>";
It's fairly rudimentary, but it works. If you have any difficulty, just reply and I'll try and help you out.

Re: Users Online

Posted: Wed Jul 20, 2011 3:13 pm
by hallsofvallhalla
nice! and always useful!

Re: Users Online

Posted: Sat Sep 17, 2011 9:01 am
by Boo
Were do i need to paste the codes?

Re: Users Online

Posted: Fri Sep 23, 2011 2:37 pm
by ConceptDestiny
Paste the top portion in your site header, and the last portion where you want the number of users to be displayed.

Re: Users Online

Posted: Fri Jan 17, 2014 8:54 pm
by Minnimous
It doesnt work, online users - still 0

Re: Users Online

Posted: Sat Jan 18, 2014 8:34 pm
by Lithium
Minnimous wrote:It doesnt work, online users - still 0
True, what ConceptDestiny forgot to say is that this bit of code is user session dependent, meaning that you need somehow some login, or user identification procedures on your site.
Also, referring "header.php" leads me to think in one or two game engines out there.

@Minnimous, I don't know if you noticed... but this is a 3 year old thread, maybe, just maybe, you have come a little too late :)