Pinging a server in php without the port?

让人想犯罪 __ 提交于 2019-12-13 00:49:41

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/15232221/pinging-a-server-in-php-without-the-port

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!