CodeIgniter Forums

Full Version: Session system
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, i want to get all my informations into my session storage into the login, i dont know how to do this.
Here is my code:

My controllers:

Code:
    public function login(){
       $this->form_validation->set_rules('username', 'Nom d\'utilisateur', 'required');
       $this->form_validation->set_rules('password', 'Mot de passe', 'required|callback_verifyUser');

       if ($this->form_validation->run() == FALSE){ // L'utilisateur arrive sur la page
           $data['title'] = 'Connexion -';
           $this->load->view('includes/header', $data);
           $this->load->view('login');
           $this->load->view('includes/footer');
       } else { // L'utilisateur à déjà rempli le formulaire et le callback verifyUser est passer
           $this->user_model->getSessionData($this->input->post('username'));
           $this->session->set_userdata($query);
           $this->load->view('includes/header', $user_data);
           $this->load->view('home');
           $this->load->view('includes/footer');
       }
   }

My Model:

Code:
       public function getSessionData($username){
           $this->db->select('*');
           $this->db->from('users');
           $this->db->where('username', $username);

           $query = $this->db->get();
           $query = $query->result_array();
           return $query;
       }
Personally I never use the userdata functions. I just access the $_SESSION variable directly. I actually thought they were deprecated but checking the session library docs I cannot confirm that - I must have been wrong.

If you have an array of user_data you can just set it like this:

PHP Code:
$_SESSION['user_info'] = $user_data

I would not use a wildcard select though, I would create my user_data array more carefully and only include the info that was absolutely necessary.

I also assume you are using a database table for sessions so you have plenty of storage space instead of using cookies.

On a final note, this appears to be a login procedure, but it seems very insecure. Also not very user friendly. After you have shown the home page after login, a refresh will cause the page to try to resubmit the login post details.

Have a look at ionauth. It is a very useful and easy to use CI library to do authorisation and authentication with many powerful features.

Best wishes,

Paul.
How can i set like this:
$_SESSION['username'] = $username
$_SESSION['administrator'] = $dbresultadministrator
Undefined variable query
Because you are doing it wrong.

PHP Code:
$query $this->user_model->getSessionData($this->input->post('username'));
$this->session->set_userdata($query->result_array()); 
I copyed your code, your logic is good but i have this error:

Type: Error

Message: Call to a member function result_array() on null

Filename: /homepages/30/d697615061/htdocs/preprod/application/controllers/Users.php

Line Number: 44
Model:

Code:
       public function login($username, $enc_password){
           $this->db->where('username', $username);
           $this->db->where('password', $enc_password);
           $result = $this->db->get('users');
           if ($result->num_rows() == 1){
               return true;
           } else {
               return false;
           }
       }

       public function getSessionData($username){
           $this->db->select('*');
           $this->db->from('users');
           $this->db->where('username', $username);
           $query = $this->db->get();
       }
   }

Controllers:

Code:
   public function login(){
        $this->form_validation->set_rules('username', 'Nom d\'utilisateur', 'required');
        $this->form_validation->set_rules('password', 'Mot de passe', 'required');

        if ($this->form_validation->run() == FALSE){ // L'utilisateur arrive sur la page
            $data['title'] = 'Connexion -';
            $this->load->view('includes/header', $data);
            $this->load->view('login');
            $this->load->view('includes/footer');
        } else { // L'utilisateur envoie le formulaire
           $enc_password = sha1($this->input->post('password'));
           $username = $this->input->post('username');
           if ($this->user_model->login($username, $enc_password)) { // Si le nom d'utilisateur et le mot de passe corresponde
               $query = $this->user_model->getSessionData($username);
               var_dump($query);
               // $this->session->set_userdata($query->result_array());
               redirect('home','refresh');
           } else { // Si les mots de passe ne correspondent pas
               $data['title'] = 'Mot de passe ou pseudo incorrect';
               $data['error'] = 'Votre nom d\'utilisateur ou votre mot de passe est incorrect';
               $this->load->view('includes/header', $data);
               $this->load->view('login', $data);
               $this->load->view('includes/footer');
           }
        }
    }


My var_dump of $query is null
Your getSessionData doesn't return anything, so of course the result, when you try to use it in your controller, is null.

At the end of your model's getSessionData...
Code:
return $query;

Oops!
You need to return the result of the query from your model.

Also have you loaded the database?
Yes the database is loaded.
The return query is for get pass the $query to the controller?

New problem:

A PHP Error was encountered
Severity: Notice
Message: session_write_close(): Skipping numeric key 0
Filename: Unknown
Line Number: 0
Backtrace:


Controller:
Code:
   public function login(){
        $this->form_validation->set_rules('username', 'Nom d\'utilisateur', 'required');
        $this->form_validation->set_rules('password', 'Mot de passe', 'required');

        if ($this->form_validation->run() == FALSE){ // L'utilisateur arrive sur la page
            $data['title'] = 'Connexion -';
            $this->load->view('includes/header', $data);
            $this->load->view('login');
            $this->load->view('includes/footer');
        } else { // L'utilisateur envoie le formulaire
           $enc_password = sha1($this->input->post('password'));
           $username = $this->input->post('username');
           if ($this->user_model->login($username, $enc_password)) { // Si le nom d'utilisateur et le mot de passe corresponde
               $query = $this->user_model->getSessionData($username);
               $array = $query->result_array();
               $this->session->set_userdata($array);
               redirect('home','refresh');
           } else { // Si les mots de passe ne correspondent pas
               $data['title'] = 'Mot de passe ou pseudo incorrect';
               $data['error'] = 'Votre nom d\'utilisateur ou votre mot de passe est incorrect';
               $this->load->view('includes/header', $data);
               $this->load->view('login', $data);
               $this->load->view('includes/footer');
           }
        }
    }

Model:

Code:
       public function getSessionData($username){
           $this->db->select('*');
           $this->db->from('users');
           $this->db->where('username', $username);
           $query = $this->db->get();
           return $query;
       }