Announcement

Collapse
No announcement yet.

Simple php status script

Collapse

728x90 top

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple php status script

    Here is a simple php code that shows if your server is online or offline

    The same principles can be used for every gameserver you want as long you know it`s ip and port


    PHP Code:

    $serverip1 
    "";  //here you add login server ip (it can be a domain also .. for eg. servername.no-ip.com)
    $serverip2 ""//here you add gameserver ip (it can be a domain also .. for eg. servername.no-ip.com)
    $port1 ""//here you add login server port
    $port2 ""//here you add game server port

    function loginip($adress){
    list(
    $ip,$port)=explode(":",$adress);
            if(
    $fp=@fsockopen($ip,$port,$ERROR_NO,$ERROR_STR,5)) {
            print 
    "up";
            
    fclose($fp);
            }else{
            print 
    "down";
            }
    }

    function 
    gameip($adress){
    list(
    $ip,$port)=explode(":",$adress);
            if(
    $fp=@fsockopen($ip,$port,$ERROR_NO,$ERROR_STR,5)) {
            print 
    "up";
            
    fclose($fp);
            }else{
            print 
    "down";
            }
    }

    loginip("$serverip1:$port1");
    gameip("$serverip2:$port2"); 

  • #2
    wd m8

    well done m8.
    ty for that one.
    some peeps still searching like hell for it.

    Comment


    • #3
      Is there a way to link it to an Iframe?

      Comment


      • #4
        How can i use it?

        Comment


        • #5
          Guide. now you know it

          laecertus ->
          Just go google.com and whrite PHP in iframe. tons of resualts!
          Hope it help. otherwice pm me.

          Dalious ->

          it's simple. just go to notebook, put in the php code. remember to add the "Start php" and "End php" tag. the code shall look like this:

          PHP Code:
          <?php
          $serverip1 
          "";  //here you add login server ip (it can be a domain also .. for eg. servername.no-ip.com)
          $serverip2 ""//here you add gameserver ip (it can be a domain also .. for eg. servername.no-ip.com)
          $port1 "2106"//here you add login server port
          $port2 "7777"//here you add game server port

          function loginip($adress){
          list(
          $ip,$port)=explode(":",$adress);
                  if(
          $fp=@fsockopen($ip,$port,$ERROR_NO,$ERROR_STR,5)) {
                  print 
          "up";
                  
          fclose($fp);
                  }else{
                  print 
          "down";
                  }
          }

          function 
          gameip($adress){
          list(
          $ip,$port)=explode(":",$adress);
                  if(
          $fp=@fsockopen($ip,$port,$ERROR_NO,$ERROR_STR,5)) {
                  print 
          "up";
                  
          fclose($fp);
                  }else{
                  print 
          "down";
                  }
          }

          loginip("$serverip1:$port1");
          gameip("$serverip2:$port2");  
          ?>


          The login server port and gameserver port is almost always those ports. (2106/7777)

          And remember you CAN'T run php from your computer. or you can, but needs a program. i prefear, Wamp. just go "google.com" whrite wamp. click search, then download and install. just click NEXT and i agree to all )
          so here is what to do point for point:

          1. (i use notebook) open notebook, put in the code i posted right up here remember to put in login/game server IP, and change the ports if they are wrong. now save the doc as what you want. E.G - 123.php

          2. download and install wamp server. when you have installed go to C:\wamp
          there are a folder named "www" go to that. put in the 123.php. there should be an index.php. dont think about that one. just put in 123.php. Now launch wamp server (if it's not launched) now you got a icon more at the right buttom. left click that, click on "Start all services" now left click agian and click on "Localhost". should be at the top. now you are at:

          Localhost.

          Now simply just put in 123.php after localhost/
          now you will see "upup" if the server is up and "downdown" if it's down.

          You can always set collers on and modify it.

          pm me if it still dont work


          //ziza

          Comment


          • #6
            It is a nice script, but it can also be used to abuse your server and network connection as well. If you have a database available on your webserver as most do, it would be wise to create a table which contains a row of data for the status of the login and game servers along with a field for date/time. Unix timestamp works great. Then in your script pull the data from that local database table, check the last update time against current time - 15 or 30 seconds. If the last update was more than 15 or 30 seconds ago, update the values in the table by running the active check on the servers, then display the values true/false.

            If you want to get fancy, you can also use your gd lib and create images on the fly from this. For instance create a GD jpg image with status and online player count, pulled from the database with a simple update if longer than 30 seconds since last time the database was updated. That can be made to look real nice and fit your website theme and still avoid the loading on the server connection of some "jerk" using your server status script to attack the server connection via your website, which obviously you do not want to block the IP of.

            Comment


            • #7
              Here, I tossed this together real quick as an example. It would need the config file created as well as the online player php file with the function to pull the online player count from the selected server (via database or telnet depending upon how you have your server setup).

              Code:
              <?PHP
              	#####################################
              	### Elfwin's GameServer Status Script ###
              	#####################################
              	
              	require "config_svrs.php";
              	include "class.inputfilter.php";
              	include "online_count.php";
              	
              	$server = $_GET['server'];
              	
              	$myFilter = new InputFilter();
              	
              	// Clean this input to avoid any cross site hijacking or sql injection attempts
              	$svr = $myFilter->process($server);
              	
              	$con = mysql_connect($host,$user,$pass);
              	if (!$con)
              	{
              	die('Could not connect: ' . mysql_error());
              	}
              
              	mysql_select_db($db, $con);
              	
              	// some fake values while we setup our vars
              	$ip = "127.0.0.1";
              	$port = "8080";
              	$svrId = 0;
              	$status = 0;
              	$playerCount = 0;
              	$lastUpdate = 0;
              	$currentTime = time();
              	
              	swithc ($svr)
              	{
              		case "bartz":
              		{
              			$svrId = 1;
              			$sName = "Bartz (PvP)";
              			break;
              		}
              		case "kain":
              		{
              			$svrId = 3;
              			$sName = "Kain (Retail)";
              			break;
              		}
              		case "gustin":
              		{
              			$svrId = 6;
              			$sName = "Gustin (Carebear)";
              			break;
              		}
              		default:
              		{
              			$svrId = 1;
              			$sName = "Bartz";
              			break;
              		}
              	}
              	
              	// ok, we've setup some vars and pulled some values.  Now let's use them!
              	$ip = get_Ip($svrId); // pulled from config
              	$port = get_port($svrId);
              	
              	
              	$sql="SELECT online_status,player_count,last_update FROM server_status WHERE server_id = '$svrId'";
              	$result = mysql_query($sql);
              	$numbersearch = mysql_numrows($result);
              	
              	if ($numbersearch == 0)
              	{
              		echo 'No Server Found<BR>';
              	}
              	else
              	{
              		$status = mysql_result($result,0,"online_status");
              		$playerCount = mysql_result($result,0,"player_count");
              		$lastUpdate = mysql_result($result,0,"last_update");
              	}
              	
              	if ($currentTime > ($lastUpdate + 30))
              	{
              		// last update of status has been too long, so let's update it now and save it.
              		if($fp = @fsockopen($ip, $port,$un,$sinn,2))
              		{
              			$status = 1;
              			$playerCount = update_player_count($svrId);
              		}
              		else
              		{
              			$status = 0;
              			$playerCount = 0;
              		}
              		
              		$lastUpdate = time();
              		
              		// Ok, the vars are updated, now save it to the database IF it found a row of data for that server
              		if ($numbersearch > 0)
              		{
              			$sql = "UPDATE server_status SET online_status = '$status', player_count = '$playerCount', last_update = '$lastUpdate' WHERE server_id = '$svrId'";
              			mysql_query($sql);
              		}
              	}
              	$statusText = "OFFLINE";
              	if ($status == 1)
              	{
              		$statusText = "ONLINE";
              	}
              	
              	
              	$text1 = $sName.' : '.$statusText;
              	$text2 = "Players: $playerCount";
              
              	$im = imagecreate(140, 40);
              	$bg = imagecolorallocate($im, 0, 22, 0);
              	$textcolor = imagecolorallocate($im, 0, 99, 0);
              
              	// write the string at the top left
              	imagestring($im, 5, 18, 2, $text1, $textcolor);
              	imagestring($im, 25, 18, 2, $text1, $textcolor);
              
              	// output the image
              	header("Content-type: image/png");
              	imagepng($im);
              
              
              	@mysql_close();
              ?>
              What that should do is as I described it in the post above. Stores the server status in a database table, then uses that with a refresh if the data is over 30 seconds old to generate a gd image on the server's status. It is very basic though, and would need some dressing up. The status is then displayed in a website by calling this php file (with server id) in a image link such as:

              Code:
              <img src="serverstatus.php?server=bartz">

              Comment


              • #8
                nice script and i do think it should be possible to link it with a iframe Dalious

                Comment


                • #9
                  The script just generates a simple jpg image, so when linking it into a website just treat it as an image and it should be just fine. The size of that image was 140 x 30, a touch bigger than a hopzone server banner in fact. And it would show the online/offline status as well as how many players you have. And thanks.

                  Comment


                  • #10
                    As well the best thing would be to keep ip/port etc. in another file like config.inc.php. And include it as well. Will be less loading for the script. Means it would stay faster, because if you would run Castle Control / 7S / Status etc. you can define most of the stuff in the config file, so less code = faster

                    Comment

                    728x90 bottom

                    Collapse
                    Working...
                    X