如果无法访问服务器,我可以使用PHP重定向到文件吗


Can I use PHP to redirect to a file if a server is unreachable?

我在家里托管自己的服务器,但我使用000webhost作为获取域并用PHP脚本将其指向我的服务器的手段(为了我的"安全",我的ISP阻止了端口80)我想知道我是否可以获取一个PHP脚本并让它ping我的web服务器,如果它关闭了,则重定向到另一个网站/文件,让我的少数用户知道它关闭了。如果PHP不能做到这一点,那么你有什么建议?

如果你想ping你的网站,请使用CURL,然后重定向。

function checkSite($url, $timeout = 80) {  
        $curl = curl_init($url);  
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);  
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
        $data = curl_exec($curl);  
        $response = curl_getinfo($curl, CURLINFO_HTTP_CODE);  
        curl_close($curl);  
    /**
     * Check if error ranges between 200 and 300
     */
    return ($response >= 200 && $response < 300) ? true : false;
}
//Check the site
if (checkSite("http://yourdomain.com")) {
    //YOUR REDIRECT HERE
}
get_headers ( string $url [, int $format = 0 ] )

http://php.net/manual/en/function.get-headers.php