Guploader Upload
Guploader Upload

Reputation: 37

Pinging a server in php without the port?

I have been having problems with a script i have been tampering with.

I am trying to make an online checker for my game server, so i can indicate if my server is online or down from my website. Here is my code:

<?php
if (!$socket = @fsockopen(IP, Port, $errno, $errstr, 2))
{
  echo "<center><img style='float:left;' src='images/offline_icon.png'><font style='float:left;' size='5' color='red'><strong>Offline!</strong></font></center>";
}
else 
{
  echo "<center><img style='float:left;' src='images/online_icon.png'><font style='float:left;' size='5' color='green'><strong>Online!</strong></font></center>";
  fclose($socket);
}
?>

My problem is, it fails. If i set the timeout to 30, it would wait thirty seconds, then mark it as offline and continue.

If i however ping the IP in terminal, it works just fine.

After searching about about ping and ports, it seems it might be the problem.

Is there a way to execute a normal ping on an IP without the port? Like linux command line?

Upvotes: 1

Views: 3792

Answers (1)

Matt
Matt

Reputation: 5428

ICMP doesn't use a port.

You might want to use exec() to ping the server. Check out: Pinging an IP address using PHP and echoing the result

Upvotes: 3

Related Questions