Codeigniter - 不通过控制器和视图工作的会话

I'm trying to make a login using sessions in codeigniter at the time the username and password match, but I can't get it. I'm doing this:

Controller:

public function __construct()
    {
        parent::__construct();
        $this->load->model('main_select');
        $this->load->helper('url');
        $this->load->library('session');

    }

    ...code when username and password match:
if($pass === $user){
        $this->session->set_userdata(array(
                                    'user_id' => $login['id_user'],
                            ));//we create the session 'user_id'
}

here it is supposed that we created a session called 'user_id' in the view it doesn't work, I have this:

if( !$this->session->userdata('id_user') ){
//see this content
//first content
}else{
//see this other
//second content
}

but I always see the same content('second content').

trying to destroy it (but not working):

public function logout()
    {
        //session_unset(); 
        // destroy the session 
        //session_destroy();
        $this->session->unset_userdata('id_user');      

        header("Location: ".base_url() );
    }

what am I doing wrong? thanks

EDIT1:

$password = md5( $this->input->post('inputpassword') ); 
$login =  $this->login_select->get_username($username);

//si no coincide
if( $login['password'] !== $password ) {}

Note : Always use database to handle user logins. (Code is related to database login check)

in your database create table with user and add this 2 fields.

  1. username
  2. password

Add some user logins to it

Then in your code

public function __construct()
    {
        parent::__construct();
        $this->load->model('main_select');
        $this->load->helper('url');
        $this->load->library('session');

    }

// logging
public function loging()
{
    $user = mysql_real_escape_string($_POST['username']);
    $pass = md5(mysql_real_escape_string($_POST['password']));

    $validate = $this->main_select->validate_user($user,$pass);

    if(empty($validate) || $validate>1)
    {
        //Not a valid user
        //redirect to login page
        $this->load->view('loging');
    }
    else
    {
        //valid user
        //set the session

        $array = array('user_id' => '$user');
        $this->session->set_userdata();
        //redirect to normal page
        $this->load->view('home_page');
    }
}

//logout
public function logout()
{

    $result= $this->session->sess_destroy(); 

    if ((isset($result))) 
    {
        header("Location: ".base_url() );
    }    
    else
    {

    }


}

In Model

public function validate_user($user,$pass)
{
    $query = $this->db->query("SELECT * FROM user WHERE username= '$user' AND password='$pass'");
    $result = $query->result_array();
    $count = count($result);
    return $count;
}

modify this line of changes then your script will work as id_user need to set first to use in script.

   if($pass === $login['password'] && $user===$login['username']){
              $this->session->set_userdata(array(
                                'id_user' => $login['id_user'],
                        ));//we create the session 'user_id'
  }

here $login['password'] and $login['username'] are data come from tables and need to change fieldname pass or user according to your user table.