Home > php教程 > PHP源码 > body text

安全的 URL 跳转

PHP中文网
Release: 2016-05-25 17:13:55
Original
1192 people have browsed it

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;
}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template