2011-06-27 25 views
0

我想通過php做300重定向,但首先我想要腳本將檢查網站是否在線,如果在線,則它會重定向,否則它將顯示無法重定向。任何人都可以告訴我怎麼可能? 感謝檢查網站後的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

謝謝Dereleased這就是我想要做的。 – DAKSH

+0

不客氣,快樂的編碼! – Dereleased