Users Online

Place to place any code snippets, completed games, or even uncompleted games for IR users to use.
Post Reply
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Users Online

Post 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.
Last edited by ConceptDestiny on Wed Jul 20, 2011 3:20 pm, edited 1 time in total.
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Users Online

Post by hallsofvallhalla »

nice! and always useful!
Boo
Posts: 9
Joined: Wed May 04, 2011 8:50 am

Re: Users Online

Post by Boo »

Were do i need to paste the codes?
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Users Online

Post by ConceptDestiny »

Paste the top portion in your site header, and the last portion where you want the number of users to be displayed.
Minnimous
Posts: 1
Joined: Sat Dec 21, 2013 3:00 pm

Re: Users Online

Post by Minnimous »

It doesnt work, online users - still 0
User avatar
Lithium
Posts: 21
Joined: Wed Jan 08, 2014 2:48 pm

Re: Users Online

Post 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 :)

Code: Select all

You laugh at me because I'm different, I laugh at you because you are all the same!
[/size]
Post Reply

Return to “Code Sharing”