php 确保url转向的实现方法

前端之家收集整理的这篇文章主要介绍了php 确保url转向的实现方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
代码首先通过head进行301转向,如果失败,则通过http-equiv="Refresh"转向,如果不行再通过js进行转向,再不行直接在页面输出链接地址用户自己点击链接转向,PHP确保url转向的方法,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

/**
 * 确保url转向
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
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 onl oad="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;
}


/***   来自编程之家 jb51.cc(jb51.cc)   ***/
原文链接:https://www.f2er.com/php/528993.html

猜你在找的PHP相关文章