首頁  >  文章  >  php教程  >  安全的 URL 跳转

安全的 URL 跳转

PHP中文网
PHP中文网原創
2016-05-25 17:13:551188瀏覽

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;
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP测试网速下一篇:PHP路由代码