[PHP] Users connected

Results 1 to 16 of 16
  1. #1
    Sorcerer Supreme Skite is offline
    Member +Rank
    Aug 2004 Join Date
    301Posts

    [PHP] Users connected

    What php function would you use to show how many people are connected to a certain server? I was planning on showing if the server was online or not, and showing how many users are there.


  2. #2
    Grand Master ismael66 is offline
    Grand MasterRank
    May 2006 Join Date
    if( _from_choice == 0 )Location
    805Posts
    mysql,mssql?
    also if it is a gaming server on which game?

  3. #3
    Grand Master kost is offline
    Grand MasterRank
    Jun 2006 Join Date
    GreeceLocation
    1,175Posts
    i have that for L2J mysql.. but for which game u need that?

  4. #4
    Sorcerer Supreme Skite is offline
    Member +Rank
    Aug 2004 Join Date
    301Posts
    It's MySQL

    Is there a way to check how many active connections there are to a port and use that to display the amount of users?

  5. #5
    Grand Master username1 is offline
    Grand MasterRank
    Jul 2004 Join Date
    5,867Posts
    This shell command will store the number of users currently connected to a certain port in a file named 'numusers'. Either put it in the crontab to run every minute or a shell script to be called from the PHP script.
    Code:
    netstat -a -n | grep port_goes_here | wc -l > numusers
    In your PHP script, use the file functions to display the contents of the file above.

  6. #6
    Sorcerer Supreme Skite is offline
    Member +Rank
    Aug 2004 Join Date
    301Posts
    These is no way to do this on pure PHP?

    and Could you chose what IP to listen on as well as what port? Because I have different servers on different computers and I would have to do this on every computer then.

  7. #7
    Grand Master FragFrog is offline
    Grand MasterRank
    Aug 2004 Join Date
    The NetherlandsLocation
    5,629Posts
    I doubt it. Apache in any case creates a new instance for each request host, and doesn't register how many instances there are, not in the $_SERVER global anyway.

    You could execute username1's script yourself without the write to file and simply turn output buffering on to directly get the data btw, but thats about as efficient as it gets :smile:

  8. #8
    Sorcerer Supreme Skite is offline
    Member +Rank
    Aug 2004 Join Date
    301Posts
    Oh. Well I have never used Shell scripts before. could you guys tell me where to put them and execute them?

  9. #9
    Grand Master username1 is offline
    Grand MasterRank
    Jul 2004 Join Date
    5,867Posts
    Learn some *nix and read this: http://www.freeos.com/guides/lsst/

  10. #10
    Newbie Zonko is offline
    MemberRank
    Jan 2007 Join Date
    16Posts
    <?php
    $string = exec("netstat -a -n | grep port_goes_here | wc -l > numusers");
    ?>

    Then you just need to preg_match/ereg the string with the REGEX and you will be able to see the numbers. :)

  11. #11
    Sorcerer Supreme Skite is offline
    Member +Rank
    Aug 2004 Join Date
    301Posts
    Quote Originally Posted by username1 View Post
    Thanks Username1, I'm going to read it in my spare time.

    Quote Originally Posted by Zonko View Post

    Then you just need to preg_match/ereg the string with the REGEX and you will be able to see the numbers. :)

    I need to what what and what? o_o?

  12. #12

  13. #13
    Grand Master FragFrog is offline
    Grand MasterRank
    Aug 2004 Join Date
    The NetherlandsLocation
    5,629Posts
    Quote Originally Posted by Zonko View Post
    <?php
    $string = exec("netstat -a -n | grep port_goes_here | wc -l > numusers");
    ?>

    Then you just need to preg_match/ereg the string with the REGEX and you will be able to see the numbers. :)
    Carefull with long exec statements, they tend to crash your server if the client is disconnect halfway trough or the script fails to run. Seems unlikely for short commands, but I've had it crash an apache server for only getting a directory listing. There are ways to let PHP continue to run while the script is being executed, as I've mentioned output buffering is one of them, I suggest you look into that :smile:

    Also, with quite static data like this its better to either get the n'th character of your string ($string[n] will do that nicely for you) or, if you have to, use strstr since its a lot faster then a regexp:
    Quote Originally Posted by PHP.net
    Tip: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.
    (And not to mention a LOT easier to get right )
    Last edited by FragFrog; 09-01-07 at 07:15 AM.

  14. #14
    Sorcerer Supreme fsadario is offline
    Member +Rank
    Apr 2005 Join Date
    Formosa, Rep. ArgentinaLocation
    360Posts
    I need a same sample but for MSSQL (Game: GunZ). I tryed <?php
    $string = exec("netstat -a -n | grep port_goes_here | wc -l > numusers");
    ?> but doesnt works. Thanks.

  15. #15
    Grand Master username1 is offline
    Grand MasterRank
    Jul 2004 Join Date
    5,867Posts

    sage

    Quote Originally Posted by fsadario View Post
    MSSQL
    Did you even read the above posts? This is *nix only. Also, you fail for not being able to figure out that "port_goes_here" is, well... nevermind.

  16. #16
    Meh Matt is offline
    Grand MasterRank
    Aug 2006 Join Date
    Ontario, CanadaLocation
    1,745Posts
    Depends what type of server....

    MuQuery = SELECT connectstat FROM MEMB_INFO WHERE connectstat='1'



Advertisement