PHP code does not work

-5

I'm trying to make a code that will appear on the screen of the "FEB ONLINE" page when the server is connected, however, it is giving error and also does not show "FEB ONLINE" when the game server is online. >

<?PHP 
$ts_ip = "177.82.148.141"; 
$ts_port = "2505"; 

$output = @fsockopen("$ts_ip", $ts_port, $errno, $errstr, 2);  
stream_set_timeout($output, 00002);

if (!$output) { 
    echo "<FONT COLOR=#DD0000><B>FEB Offline</B></FONT>"; 
} else { 
    echo "<FONT COLOR=#00DD00><B>FEB Online</B></FONT>"; 
} 
@fclose($output); 
?>
  

Warning: stream_set_timeout () expects parameter 1 to be resource,   boolean given in /home/u918484727/public_html/teste.php on line 6

    
asked by anonymous 05.05.2017 / 19:55

1 answer

2

Just read the error expects parameter 1 to be resource, boolean given , translating would:

  

Parameter 1 was expected to be a resource, but it obtained a Boolean

That is fsockopen got false and as you used @ you did not see the error, so adjust to:

$output = fsockopen($ts_ip, $ts_port, $errno, $errstr, 2);

if (!$output) { 
    echo "<FONT COLOR=#DD0000><B>FEB Offline</B></FONT>"; 
} else { 
    echo "<FONT COLOR=#00DD00><B>FEB Online</B></FONT>";
    fclose($output);
}

I recommend that you do not use arroba, in production you prefer to set display_errors to Off , I recommend you read this:

Note that $errno and $errstr are references , in them you can get details of the connection error, for example:

$output = fsockopen($ts_ip, $ts_port, $errno, $errstr, 2);

if (!$output) { 
    echo "<FONT COLOR=#DD0000><B>FEB Offline ($errstr - $errno)</B></FONT>"; 
} else { 
    echo "<FONT COLOR=#00DD00><B>FEB Online</B></FONT>";
    fclose($output);
}

Or even customize the message, depending on the error.

Timeout

If you get an error like:

  Warning: fsockopen (): unable to connect to 177.82.148.141:2505 (Connection timed out)

It's because you could not connect within the given time, in case you used 2 seconds:

$output = fsockopen($ts_ip, $ts_port, $errno, $errstr, 2);

Try adjusting for 10 seconds (I think it's ideal) or more:

$output = fsockopen($ts_ip, $ts_port, $errno, $errstr, 10);

If you do not try a value greater than 15 up to 30, greater than 30 I find unnecessary, if you use 30 seconds and the error occurs then the problem is on the server, the reasons can be:

  • Firewall
  • Is in a closed network
  • The address or port is incorrect
  • The server you tried to access is offline

Releasing a door on the internet

The port you are trying to release was not "propagated" to the internet, it is only running inside the computer network, but before the internet you have the router or modem, you have to configure them so you can free the port on the internet, as I explained in link , you must configure the modem or router (depends on how the network is), if it is a simple Wan + Lan network, without intermediaries just do this:

  

If the network is very complex, with many waterfalls I recommend not to do this, look for a professional in the area of networks so that he can do this for you, because touching this can give a lot of headaches

  • Set the IP of your machine / computer (to avoid ip changes by DHCP)
  • Look at your router for VirtualServer (each router is in a way you can not specify)
  • There should be a location typed "Foward" , in this field enter your local machine IP and no for example 192.168.0.10)
  • There are probably 3 or 5 fields:
    • Protocol / Type: Select TCP (depends on server type)
    • Port Start: Enter 2505 (both local and external )
    • Port End: Enter 2505 (both local and external )
    • In Port Start and Port End for external you can put another port (usually 80 is blocked by your ISP or already configured on the router). Restart the router (not always necessary)
  • If IP 177.82.148.141 is dynamic then I recommend using No-IP, details at: link
  • 05.05.2017 / 20:00