Codeigniter implementation handles URL jump after user login authentication

  • 2021-07-01 06:44:47
  • OfStack

Codeigniter handles URL jump after user login verification, which mainly involves My_Controller. php page and login verification module User.php page. The specific code is as follows:

My_Controller. php page:

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        /* Judge whether to log in or not, and judge the current URL Whether it is auth/login*/
        if ( ! $this->tank_auth->is_logged_in()
                && ( $this->router->fetch_class() != 'auth' && $this->router->fetch_method() != 'login'))
        {
            $redirect = $this->uri->uri_string();

            if ( $_SERVER['QUERY_STRING'])
            {
                $redirect .= '?' . $_SERVER['QUERY_STRING'];
            }
            /* Jump to the user login page and specify Login Backward jump URL*/
            redirect('auth/login?redirect='.$redirect);
        }    
    }
}

User. php page:

class User extends MY_Controller 
{
    function login()
    {
        if ($this->tank_auth->is_logged_in()) {                                    // logged in
            redirect('/');
        } else {
            //other codes here......
            /* Determine if there is redirect Information */
            $data['redirect'] = isset($_GET['redirect']) ? $_GET['redirect'] : '/';
            if ($this->form_validation->run()) {                                // validation ok
                if ($this->tank_auth->login(
                        $this->form_validation->set_value('login'),
                        $this->form_validation->set_value('password'),
                        $this->form_validation->set_value('remember'),
                        $data['login_by_username'],
                        $data['login_by_email'])) {                                // success
                    redirect($data['redirect']);
                } else {
                    //error handling
                }
            }
            $this->load->view("login_form")
        }
    }
/*
Note:  In login_form Note that the form Address: 
<?php echo form_open(site_url("/auth/login?redirect=".$redirect)); ?>
*/
}

Note in login_form that the form address for submitting the form is:

<?php echo form_open(site_url("/auth/login?redirect=".$redirect)); ?>


Related articles: