首页 >php教程 >PHP源码 >安全的 URL 跳转

安全的 URL 跳转

PHP中文网
PHP中文网原创
2016-05-25 17:13:551195浏览

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路由代码