PHP code does not work

Asked

Viewed 91 times

-5

I am trying to make a code that will appear on the screen of the page "FEB ONLINE" when the server is on, however, 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

  • 5

    The error is in the previous line, the socket is not opening. Remove the @ code and error should appear with the real reason. Please update the question with the error message that appears.

  • Fatal error: Call to Undefined Function sockopen() in /home/u918484727/public_html/index.php on line 5

  • 1

    Looks like you removed one f besides the @.

  • Warning: stream_set_timeout() expects Parameter 1 to be Resource, Boolean Given in /home/u918484727/public_html/index.php on line 6

1 answer

2


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

Parameter 1 was expected to be a Resource, but got a boolean

That is to say fsockopen got false and how he used the @ you didn’t 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 not to use the arroba, in production prefer to adjust the display_errors for Off, recommend you read this:

Note that $errno and $errstr sane references, in them you can get details of the error in connection, 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 is 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 to 10 seconds (I think it’s ideal) or more:

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

If you do not give try a value greater as 15 until 30, larger than 30 I think unnecessary, if you use 30 seconds and the error occurs then the problem is in the server, the reasons may be:

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

Release a port on the internet

The port you are trying to release was not "propagated" for the internet, it is running only inside the computer network, but before the internet you have the router or modem, you have to configure them so you can release the port on the internet, as I explained in /a/50934/3635, 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 cascades I recommend not to do this, look for a professional in the area of networks so that he does it for you, because messing with it can give many headaches

  1. Fix the IP of your machine/computer (to avoid IP changes by DHCP)
  2. Search your router for VirtualServer (each router is in a way has no way to specify)
  3. There must be a place written "Foward", in this field type your Local IP of your machine and nay the fixed internet (for example 192.168.0.10)
  4. There are probably 3 or 5 fields:
    • Protocol/Type: Select TCP (depends on server type)
    • Port Start: Type 2505 (both no local how much in the external)
    • Port End: Type 2505 (both no local how much in the external)
    • In the Port Start and Port End for external you can put another port (usually 80 is blocked by the ISP or already comes configured on the router). Restart the router (not always required)
  5. If IP 177.82.148.141 is dynamic then I recommend using No-IP, details at: /a/50934/3635

Browser other questions tagged

You are not signed in. Login or sign up in order to post.