在没有端口的情况下在php中ping服务器? [英] 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);
}
?>
我的问题是,它失败了.如果我将超时设置为30,则将等待三十秒钟,然后将其标记为脱机并继续.
My problem is, it fails. If i set the timeout to 30, it would wait thirty seconds, then mark it as offline and continue.
但是如果我在终端中ping IP,它就可以正常工作.
If i however ping the IP in terminal, it works just fine.
搜索有关ping和端口的信息后,看来可能是问题所在.
After searching about about ping and ports, it seems it might be the problem.
有没有一种方法可以在没有端口的情况下对IP执行正常的ping操作?像linux命令行一样?
Is there a way to execute a normal ping on an IP without the port? Like linux command line?
推荐答案
ICMP不使用端口.
ICMP doesn't use a port.
您可能想使用exec()
ping服务器.检出:使用PHP ping IP地址并回显结果
You might want to use exec()
to ping the server. Check out: Pinging an IP address using PHP and echoing the result
这篇关于在没有端口的情况下在php中ping服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!