如何重定向登录后的人


How to redirect a person after loggin

我使用下面的函数来重定向特定任务后的人(例如::登录后、退出后、搜索后等)代码如下:

<?php
class common {
    /* Redirect to another page
     * $url= Url to go
    */
    function redirection($url){
        header("location: $url");
        exit();
    }
    // Some other function below
?>

但是现在我正在处理这个类不同主机的许多项目(MLM项目)。我现在有个问题。在一些服务器上,它像我预期的那样工作,但在其他一些服务器上,它没有重定向。如果我使能error_reporting(E_ALL);我发现一个通知,headers are already send。所以我很困惑,我现在可以做什么来代替header()函数。我还尝试了下面的代码

<?php
    function redirection($url){
        echo "<div align='center'><a href='$url' target='_top'><img src='../img/proceed.jpg' alt='Proceed>>' align='absmiddle' border='0'></a></div>";
        exit();
    }
?>

但这是不可取的,因为每个人都希望自动重定向。我的服务器都是windows和linux。谁来帮帮我

嗯,这种情况很常见,那么您可以简单地打开输出缓冲(输出将存储在内部缓冲区中)。

在应用程序的第一行使用ob_start();

<?php
    class common {
        /* Redirect to another page
         * $url= Url to go
         */
        function redirection($url)
        {
          header("location: $url");
          exit();
        }
        // Some other function below
    }
?>

<?php
    ob_start("redirection");
    // Your Common Class Page
    include("Common.php");
     // some code 
    ob_end_flush(); // turn off output buffering
?>

处理这个问题的一种方法是在调用header(location)之前测试头部是否已经发送。你可以混合使用两种解决方案:

<?php
class common {
    /* Redirect to another page
     * $url= Url to go
    */
    function redirection($url){
        if (!headers_sent()) {
            header("location: $url");
        } else {
            echo "<div align='center'><a href='$url' target='_top'><img src='../img/proceed.jpg' alt='Proceed>>' align='absmiddle' border='0'></a></div>";
        }
        exit();
    }
// Some other function below
?>

这样,如果标题还没有发送,你会自动重定向。如果有,请客户端点击。

这就是为什么当你在大多数网站上看到重定向通知时,它还包括一句话:如果你没有被自动重定向,请点击这里…

希望对你有帮助。

祝你好运!

如果标题已经发送,很可能是因为内容已经被写到了屏幕上(通过回显、打印或类似的方式)。由于您的类无法控制在它被实例化和函数被调用之前发生的事情,因此您似乎不太可能做很多事情来避免客户机PHP(调用您的类的东西)在之前编写任何内容。使用Javascript或使用Apache重定向。

我会尝试使用:

header("Location: ".$url, TRUE, 302);

如果你想使用一个不同的方法,或者叫做"refresh"方法,

header("Refresh:0;url=".$url);

两者在任何情况下都有效。你的头的问题是,你需要让他们知道这是一个302重定向,以及设置TRUE来取代现有的头。如果header已经设置,您需要使用TRUE布尔值替换它。

302也是用于重定向的通用HTTP响应代码,当您尝试使用header重定向时需要指定。

刷新方法也可以很好地工作,尽管它与旧的浏览器存在兼容性问题。

http://en.wikipedia.org/wiki/HTTP_302

http://php.net/manual/en/function.header.php

最简单的方法是通过客户端完成。javascript…

window.location= url