首頁 > php教程 > PHP源码 > 主體

安全的 URL 跳转

PHP中文网
發布: 2016-05-25 17:13:55
原創
1187 人瀏覽過

php代码

function safe_redirect($url, $exit=true) {
 
    // Only use the header redirection if headers are not already sent
    if (!headers_sent()){
 
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $url);
 
        // Optional workaround for an IE bug (thanks Olav)
        header("Connection: close");
    }
 
    // HTML/JS Fallback:
    // If the header redirection did not work, try to use various methods other methods
 
    print &#39;<html>&#39;;
    print &#39;<head><title>Redirecting you...</title>&#39;;
    print &#39;<meta http-equiv="Refresh" content="0;url=&#39;.$url.&#39;" />&#39;;
    print &#39;</head>&#39;;
    print &#39;<body onload="location.replace(\&#39;&#39;.$url.&#39;\&#39;)">&#39;;
 
    // If the javascript and meta redirect did not work, 
    // the user can still click this link
    print &#39;You should be redirected to this URL:<br />&#39;;
    print "<a href="$url">$url</a><br /><br />";
 
    print &#39;If you are not, please click on the link above.<br />&#39;;    
 
    print &#39;</body>&#39;;
    print &#39;</html>&#39;;
 
    // Stop the script here (optional)
    if ($exit) exit;
}
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板