最好的重定向方法? [英] Best redirect methods?

查看:171
本文介绍了最好的重定向方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个PHP文件中,当我需要重定向一个用户并且头文件已经发送了,所以我不能使用php的头文件功能,在这种情况下,这是重定向用户的最好方法吗?



不管用户的浏览器品牌如何,最快和最可靠的方法?

  echo'< script type =文本/ JavaScript的 > window.top.location = HTTP://本地主机/;< /脚本>'; 

// OR

echo'< meta http-equiv =refreshcontent =0; url ='。$ location。'>';

更新

这里是我现在使用的最终结果代码,如果标题已经发送到无法重定向到主页的地方,我只需将主页带到我的页面,而不是包含正文页面,它将包含我的主页和页脚

 函数validlogin($ url){
if(!isset($ _ SESSION ['auto_id'])||( $ _SESSION ['auto_id'] =='')){
$ _SESSION ['sess_login_msg'] ='请登录';
$ _SESSION ['backurl'] = $ url;
$ temp ='';
if(headers_sent()=== false){
header(Location:/);
exit();
} else {
// header已经发送,所以我们只需要把主页带给我们而不是去!
包含'mainbody.inc.php';
包含'footer.inc.php';
exit();




解决方案

 函数重定向($ url,$ permanent = false)
{
if(headers_sent()=== false)
{
header('Location:'。$ url,true,($ permanent === true)?301:302);
}

exit();
}

重定向('http://www.google.com/',false);

再次使用ob_start();


In a PHP file when I need to redirect a user and headers are already sent so I can not use php's header function, in this case which is the best method to redirect a user?

Fastest and most reliable method regardless of the users browser brand?

echo '<script type="text/javascript">window.top.location="http://localhost/";</script>';

// OR

echo '<meta http-equiv="refresh" content="0;url=' .$location. '">';

UPDATE

Here is my end result code I am using now, if headers are already sent where I cannot redirect to the homepage, I just bring the homepage to me, so instead of including the body page, it will include my homepage and footer

function validlogin($url) {
    if (!isset($_SESSION['auto_id']) || ($_SESSION['auto_id']=='')) {
        $_SESSION['sess_login_msg'] = 'Please login';
        $_SESSION['backurl'] = $url;
        $temp = '';
        if (headers_sent() === false){
            header("Location: /");
            exit();
        }else{
            //header is already sent so we will just bring the homepage to us instead of going to it!!!
            include 'mainbody.inc.php';
            include 'footer.inc.php';
            exit();
        }
    }
}

解决方案

function Redirect($url, $permanent = false)
{
    if (headers_sent() === false)
    {
        header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
    }

    exit();
}

Redirect('http://www.google.com/', false);

Again, use ob_start();

这篇关于最好的重定向方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆