Welcome Guest, Not a member yet? Register   Sign In
use hooks to authentication, but everytime needs to login twice
#1

[eluser]young[/eluser]
I use post_controller_constructor to check session if the user can access the system.
But I have to input the user name and password twice to login.
The first time I login, it will redirect to the login page to let me input the info again.
But if I login out, I'm sure the session is clean, and I only need to login once.
Also I have to point that, one weeks ago, the IE works well(login once), when I use Firefox, I need to login twice.
But now, the firefox works well, I have to login twicw under IE...

who can help me?...

I feel I was dying, poor system....

hooks.php
Code:
$hook['post_controller_constructor'] = array(
                                'class'    => 'Validate',
                                'function' => 'validate_user',
                                'filename' => 'validate.php',
                                'filepath' => 'hooks'
);

validate.php
Code:
class Validate {
    function validate_user(){
        $uri =& load_class('URI');
        $type = $uri->segment(1);
        $param = $uri->segment(2);

        if($type == "admin" && !empty($param)){
            if(!isset($_SESSION['user'])){ // [color=red]I check session here[/color]
                $CI = & get_instance();
                $CI->load->helper('url');
                redirect('/admin');
            }
        }
    }
}

admin_controller.php
Code:
class Admin_controller extends Controller {
    function __construct(){
        parent::Controller();
        $this->load->model('user_model');
        $this->load->helper('url');
    }

    function index($type = ""){
        if($type == "login_out"){
            unset($_SESSION['user']);
            redirect('/admin');
        }else if($type == "home"){
            $this->load->view('admin/home/home.php');
        }else{
            if(isset($_SESSION['user'])){
                redirect('/admin/home');
            }else{
                $user_name = $this->input->post('user_name');
                $password = $this->input->post('password');
                if($user_name !== FALSE && $password !== FALSE){
                    $stat = $this->user_model->validate_user($user_name, $password);
                    if($stat === TRUE){
                        $_SESSION['user'] = $user_name;    // [color=red]I set session here[/color]

                        redirect('/admin/home');
                    }else{
                        echo "error on login, no such user or error password";
                        $this->load->view('admin/login.php');
                    }
                }else{
                    $this->load->view('admin/login.php');
                }
            }
        }
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB