【发布时间】:2013-03-15 18:35:37
【问题描述】:
我正在尝试让一个基本的 php ping 脚本正常工作,该脚本将 ping 我的网站 ip,如果它响应会将用户重定向到我的网站,但如果没有,它将把用户重定向到其他地方。我在下面附上了我到目前为止所拥有的内容,但目前它似乎没有在 ping 站点,因为它直接到 http://othersite.com,如下例所示。感谢您提供的任何帮助。
<?php
function ping($host)
{
exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, $rval);
return $rval === 0;
}
// random host ip example
$host = '8.8.8.8';
$online = ping($host);
// if site is online, send them to the site.
if( $online ) {
header('Location: mysite.com');
}
// otherwise, lets go elsewhere to an online site
else {
header('Location: http://othersite.com/');
}
?>
【问题讨论】:
-
你知道不同的输出吗?从 Linux 机器 ping 时?和Windows机器?如果是这样,则捕获您的输出,然后对关键短语采取行动,以便您的脚本可以识别成功或失败
-
我没想到它是一个正在 ping 的 linux vps 我会根据这个修改脚本谢谢 :)