[Tutorial] How to add Online Users

Post all your tuts or request for tuts here.
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

[Tutorial] How to add Online Users

Post by vitinho444 »

Hey guys, so im here to teach you how to add Online Users.

First i need to say this: I got this idea when i was in the bathroom, pretty stupid han?
xD

So lets start.

First make a new file called info.php

Then Type this into it:

Code: Select all

<?php
include_once 'connect.php';
 session_start();
   include_once 'logo.php';
   ?>
     <link href="style.css" rel="stylesheet" type="text/css" />
<?php

  $player=$_SESSION['player'];

$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);

?>
Explaining the code:

So this is pretty basic, so this will catch the player info and stuff.

Now insert a new filed to your Players Table in the DB called online and make it SMALLINT and dont set a default value.


Now go to your Auth.php (Authenticate.php)
And add this to it after the part saying
Welcome back
or something.

Code: Select all

$isonline="update players set online='1' where name='$name'";
mysql_query($isonline) or die("The Server is getting Errors, Please Contact the Admins with the following error code: 981A");
The Server is getting Errors, Please Contact the Admins with the following error code: 981A
this was my idea just to know the error if it occurs, you can edit as you want.

So this in your Authenticate page will set the online field to 1, saying that you are online.


Now go to your Logout.php and add before
session_destroy();
this:

Code: Select all

$isonline="update players set online='0' where name='$name'";
mysql_query($isonline) or die("The Server is getting Errors, Please Contact the Admins with the following error code: 981A");
now this will say that you are offline and set the field online to 0 (zero).

now delete this in your logout.php:
session_start();
this i dont know why its there but when i made this system, xampp was giving me annoying messages about the session_start and stuff so, deleting it its no big deal...



So now you got the "timer" checking if you are online or offline.

Uses:

Now you can use this "system" for a lot of things in your game like the rank page (you can check how to make one HERE , Tut by me)

to add to it just follow the Tut and at the end i explain to you how to add new fields to the page.

Well, the tut ends here :D
hope its usefull to you people, and sorry if its too simple but it's easy to the beginners to understand ...



You dont need to credit me.


CREDIT INDIE-RESOURCE FOR ALL THE HELP THEY ARE GIVING TO YOU
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
mrmajic
Posts: 117
Joined: Sat Jun 27, 2009 9:37 am

Re: [Tutorial] How to add Online Users

Post by mrmajic »

2 things .. if you delete the start_session() from the logout page, then how does the logout script know which user to set logged_in = 0? also, what if someone doenst logoff, they just close their browser? then they are known as been online?
Mr Majic...

Current Project: http://www.deltarealm.net
feedback welcome.
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: [Tutorial] How to add Online Users

Post by ConceptDestiny »

Very nice! :)

I prefer creating a seperate table called user_online to handle users online, while using the following code in the header or your global connect file to update it:

Code: Select all

   <?php
      if (isset($session_username))
      {      
              $session=session_id();
              $time=time();
              $time_check=$time-600;
              
              // SESSION COUNT ONLINE USERS
              $sql="SELECT * FROM user_online WHERE session='$session'";
              $result=mysql_query($sql);    
              $count=mysql_num_rows($result);
              
              if($count=="0")
              {
                $sql1="INSERT INTO user_online(session, time, user)VALUES('$session','$time','$username')";
                $result1=mysql_query($sql1);
              }
              else 
              {
                "$sql2=UPDATE user_online SET time='$time' WHERE session='$session'";
                $result2=mysql_query($sql2);
              }
              
              $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);
      }        
            $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>";    
    ?>
The output at the end is far from efficient, but feel free to customise it for your own use. :)
User avatar
JinkX
Posts: 18
Joined: Sat Apr 30, 2011 9:09 am

Re: [Tutorial] How to add Online Users

Post by JinkX »

mrmajic wrote:2 things .. if you delete the start_session() from the logout page, then how does the logout script know which user to set logged_in = 0? also, what if someone doenst logoff, they just close their browser? then they are known as been online?
if u just close your browser the session will get destroyed after some time. just the same way it will be if ur inactive on any other page.

and why exactly do u want to delete the start_session() from the logoout page?
another game development website
www.gamedevforums.org
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: [Tutorial] How to add Online Users

Post by vitinho444 »

well i deleted it because it was giving me warnings...

if someone put it there please help me fixing the error
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
JinkX
Posts: 18
Joined: Sat Apr 30, 2011 9:09 am

Re: [Tutorial] How to add Online Users

Post by JinkX »

you should not have deleted that!

what was the warning ?

session already started - ignoring session start?

if that was ur error u could at least do this

Code: Select all

@session_start();
that will suppress the warning messages, but will not fix the problem.
another game development website
www.gamedevforums.org
toby
Posts: 3
Joined: Wed May 04, 2011 2:24 pm

Re: [Tutorial] How to add Online Users

Post by toby »

Would this stop the avatars from appearing if you are offline?
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: [Tutorial] How to add Online Users

Post by ConceptDestiny »

toby wrote:Would this stop the avatars from appearing if you are offline?
How do you mean?
User avatar
JinkX
Posts: 18
Joined: Sat Apr 30, 2011 9:09 am

Re: [Tutorial] How to add Online Users

Post by JinkX »

this was removed it wont only stop avatars from appearing, it will crash ur page and nothing will be appearing on ur screen but a big ERROR. :D
another game development website
www.gamedevforums.org
toby
Posts: 3
Joined: Wed May 04, 2011 2:24 pm

Re: [Tutorial] How to add Online Users

Post by toby »

Oh I just meant would it stop the image appearing for the player, but I assume it does.
Post Reply

Return to “Tutorials”