简体   繁体   English

如果用户未使用构造函数进行身份验证,则无法重定向到登录页面

[英]Cannot redirect to login page if user not authenticated using constructor

I have an Admin controller which handles the user login and other admin functionality like managing a basic cms. 我有一个Admin控制器,可以处理用户登录和其他管理功能,例如管理基本cms。 In the constructor I check if the users session present which is set once they login. 在构造函数中,我检查用户登录后是否设置了用户会话。 If not and they try to access a restricted page, they should be redirected. 如果没有,并且他们尝试访问受限页面,则应将其重定向。 If I redirect to another page then when going to the login page I get redirected right away as obviously that check is done in the constructor, but as soon as I try to redirect to the login page I get an error: 如果我重定向到另一个页面,那么当转到登录页面时,我会立即重定向,因为显然检查是在构造函数中完成的,但是当我尝试重定向到登录页面时,我得到一个错误:

This page isn't working localhost redirected you too many times. 此页面无法正常运行localhost重定向您太​​多次。 Try clearing your cookies. 尝试清除您的cookie。 ERR_TOO_MANY_REDIRECTS ERR_TOO_MANY_REDIRECTS

class Admin extends Controller {

    public function __construct()

    {

        if(!isLoggedIn()) {

            redirect('admin/login');
        }

        $this->AuthModel = $this->model('Auth');

    }

isLoggedIn is a function: isLoggedIn是一个函数:

function isLoggedIn() {

    if(isset($_SESSION['user_id']) && isset($_SESSION['browser']) && $_SESSION['browser'] == $_SERVER['HTTP_USER_AGENT']) {

        return true;

    } else {

        return false;
    }
}

Login method: 登录方式:

public function login()
{
if($_SERVER['REQUEST_METHOD'] == 'POST') {

    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    $password = trim($_POST['password']);

    $data = [

    'email' => $email,
    'password' => $password,
    'email_error' => '',
    'pass_error' => '',
    'no_user' => '',
    'pass_wrong' => ''

    ];

    if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

        $data['email_error'] = 'Invalid email address <br />';
    }

    if(empty(trim($_POST['password']))) {

        $data['pass_error'] = 'Password required';
    }

    if(!empty(trim($_POST['email'])) && !$this->AuthModel->getUserByEmail($email)) {

        $data['no_user'] = 'Email does not exist';
    }


    if(!empty($data['email_error']) || !empty($data['pass_error']) || !empty($data['no_user'])) {

        $this->view('admin/login', $data);

    } else {

        $loggedInUser = $this->AuthModel->login($email, $password);

        if($loggedInUser) {

            $this->CreateUserSession($loggedInUser);


        } else {

            $data['pass_wrong'] = 'Incorrect login details';
            $this->view('admin/login', $data);
        }
    }

} else {

    $data = [

        'email' => '',
        'password' => '',
        'email_error' => '',
        'pass_error' => '',
        'no_user' =>'',
        'pass_wrong' => ''
    ];

    $this->view('admin/login', $data);
  }
}

You are using same controller for login and other functionalities. 您正在使用同一controller进行login和其他功能。 Every time it checks for authentication and it gets no. 每次它检查authentication时都不会。 And it loops over again. 并且它再次loops try to implement your login functionalities in a separate Controller . 尝试在单独的Controller实现您的登录functionalities

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM