简体   繁体   中英

Page is not working — ERR_TOO_MANY_REDIRECTS

When this code implement in codeigniter (this type of error show => localhost redirected you too many times.)

1)application/config/config.php

    $config['enable_hooks'] = TRUE;

2)application/config/hooks.php

    $hook['post_controller_constructor'][] = array(
        'function' => 'checkAdminLogin',
        'filename' => 'authenticate_login.php',
        'filepath' => 'hooks',
    );

3)application/hooks/authenticate_login.php (make new php file (authenticate_login.php))

    function checkAdminLogin() {
        $CI = & get_instance();
        if (!getSession('is_logged_in')) {
            redirect('admin/login');
        } else
            return true;
    }

    function getSession($key) {
        $CI = & get_instance();
        return $CI->session->userdata(trim($key));
    }

When your visitor goes to admin/login your code will detect that he is not logged in and redirect it again to admin/login....and over, and over again...

You must detect login page route and not redirect from it.

Something like:

function checkAdminLogin() {
    $CI = & get_instance();
    if (!getSession('is_logged_in'))
    {
        if ($_SERVER['REQUEST_URI']!='/admin/login') {
            redirect('admin/login');
        }else{
            // User is on login page, don't redirect him again
        }
    } else
        return true;
}

But I'm not sure what returning true means in your case. Basically you have 3 cases here:

  1. Logged in
  2. Not logged in, but on login page
  3. Not logged in and not on login page

So you have to think of all those 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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