龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php 安全的 URL 跳转

时间:2014-09-17 15:13来源:网络整理 作者:网络 点击:
分享到:
安全的 URL 跳转 [代码片段(35行)]
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 '<html>';
    print '<head><title>Redirecting you...</title>';
    print '<meta http-equiv="Refresh" content="0;url='.$url.'" />';
    print '</head>';
    print '<body onload="location.replace(\\''.$url.'\\')">';

    // If the javascript and meta redirect did not work, 
    // the user can still click this link
    print 'You should be redirected to this URL:<br />';
    print "<a href="$url">$url</a><br /><br />";

    print 'If you are not, please click on the link above.<br />';    

    print '</body>';
    print '</html>';

    // Stop the script here (optional)
    if ($exit) exit;
}
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接