2011-06-27 12 views
0

私はPHPでリダイレクトしたいと思っていますが、最初にスクリプトがサイトがオンラインであるかどうかをチェックしたい場合は、オンラインの場合はリダイレクトします。誰にどのように可能か教えていただけますか? (betternessのために編集) おかげウェブサイトをチェックした後にPHPリダイレクト

+3

は定義 "オンライン。" HTTPリクエストの '200 OK'を'/'に返しますか? – Matchu

答えて

2

これはそれを行う必要があります

$destination = 'http://www.google.com'; 
$ch = curl_init($destination); 
// use request type HEAD because it's faster and lighter 
curl_setopt($ch, CURLOPT_NOBODY, true); 
// prevent curl from dumping any output 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// prevent curl from producing any output 
curl_setopt($ch, CURLOPT_HEADER, false); 
// run request 
curl_exec($ch); 
// consider request a success if the HTTP Code is less than 400 (start of errors) 
// change this to whatever you expect to get, e.g. "equal to 200 (OK)" 
$success = (bool) ((int)curl_getinfo($ch, CURLINFO_HTTP_CODE) < 400); 
curl_close($ch); 
// redirect or die 
if ($success) { 
    header('Location: ' . $destination, true, 301); 
} else { 
    die('Unable to redirect to <a href="'.$destination.'">'.$destination.'</a>'); 
} 
+0

ありがとうございましたそれは私がやろうとしていたものです。 – DAKSH

+0

よろしくお願いします。 – Dereleased

関連する問題