首页 > 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
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板