将页面重定向到根路径

0 php

我在这里有这个类,我想要做的是,如果检查某些东西等于false,那么用户将被重定向到根域路径.但它不起作用.这是班级

class security {
    function checkAuth() {
        if(isset($_COOKIE['AUTHID'])) {
            $cookie = $this->secure($_COOKIE['AUTHID']);
            $query = mysql_query("select username,password,active from tbl_users where password = '$cookie'") or die(mysql_error());
            while($row = mysql_fetch_assoc($query)) {
                //check if cookie is set
                if(!isset($_COOKIE['AUTHID'])) {
                    header("Location: ".realpath($_SERVER['HTTP_HOST']));
                }

                //check if user is active
                if($cookie == $row['password']) { 
                    if($row['active'] == '0') {
                        setcookie("AUTHID","",time() - 100000);
                        header("Location: ".realpath($_SERVER['HTTP_HOST']));
                    }
                    else { //user is active
                    }
                }
                //check if hash in cookie matches hash in db
                if($cookie != $row['password']) { 
                    setcookie("AUTHID","",time() - 100000);
                    header("Location: ".realpath($_SERVER['HTTP_HOST']));
                }
            }
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

Ant*_*thy 7

  1. 我认为在一个类中重定向/直接输出不是一个好主意,原因很多,最重要的是它无视OO的全部要点.而是返回false并让调用脚本执行重定向.
  2. 你需要发送标题作为你做的第一件事,如果PHP已经开始输出文本,因为标题已经发送,基于标题的重定向将不起作用.

尝试

$_SERVER['SCRIPT_URI'];
Run Code Online (Sandbox Code Playgroud)

要么

"http://" . $_SERVER['HTTP_HOST'];
Run Code Online (Sandbox Code Playgroud)

是的,退出(); 发送该标题后

不要忘记为重定向发送适当的30x 标头响应代码


Mil*_*kov 5

为什么不简单:

header('Location: /');
Run Code Online (Sandbox Code Playgroud)