Php 如何在登录后重定向一个人

Php 如何在登录后重定向一个人,php,http-headers,url-redirection,Php,Http Headers,Url Redirection,我正在使用下面的功能在特定任务后重定向一个人(例如:登录后、注销后、搜索后等) 代码如下: <?php class common { /* Redirect to another page * $url= Url to go */ function redirection($url){ header("location: $url"); exit(); } // Some other function bel

我正在使用下面的功能在特定任务后重定向一个人(例如:登录后、注销后、搜索后等) 代码如下:

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

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

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

window.location= url
我会尝试使用:

header("Location: ".$url, TRUE, 302);
如果要使用不同的方法,或称为“刷新”方法

这两种方法在任何情况下都有效。标题的问题是,您需要让他们知道这是302重定向,并设置TRUE以替换现有标题。如果已经设置了标头,则需要使用TRUE布尔值替换它

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

刷新方法也可以正常工作,尽管它与旧浏览器存在兼容性问题


如果标题已经发送,很可能是因为内容已经写入屏幕(通过回音、打印或类似方式)。由于您的类无法控制在实例化和调用函数之前发生的事情,因此您似乎不太可能避免您的客户机PHP(调用您的类的内容)在之前写出任何东西。要么使用Javascript,要么使用

处理此问题的一种方法是在调用header(位置)之前测试header是否已发送。您可以混合使用这两种解决方案:

<?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
?>

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

使用
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
?>


在执行
头()
之前,是否已检查以确保没有输出任何内容?另外,请注意关闭
?>
php标记后的空格或换行符。“请记住,在发送任何实际输出之前,必须调用header(),无论是通过普通HTML标记、文件中的空行还是从php。使用include()或require()读取代码是非常常见的错误,函数或其他文件访问函数,并在调用header()之前输出空格或空行。使用单个PHP/HTML文件时也存在同样的问题。“如果启用错误报告(E_ALL),您可能会收到将发送到浏览器的警告,这就是为什么会出现此错误“header ready sent”。注意:要在PHP中实现此功能,可能需要使用ob_start()和ob_end_flush()。。。您可以在这里阅读:您可能希望在include(common.php)之前移动ob_start。正如您在回答中所说,它应该用在应用程序的第一行。@jdias是的,我现在编辑了答案,它在正确的位置。谢谢你的观察。@Indian Girl如果对你有用就好了。就像我说的,这是一种常见情况,如果愿意,可以深入一点,在公共类中执行一些验证(验证是否已经发送了头文件),如果是,则打开输出缓冲,否则只需使用头文件重定向(“Location your_page.php”);如果它真的对你有帮助,请接受答案。谢谢,祝你在项目中好运。事实上,第一个通过TRUE的选项是行不通的。如手册中所述,默认值为TRUE,因此传递TRUE不会更改默认行为。我相信这个选项是用来处理您尚未发送的标题更改的。
<?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
    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
?>