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>";


