Rohan Peters
Rohan Peters

Reputation: 147

Authentication with CodeIgniter - Additional Session Information

I have been an avid reader of stackoverflow for a long period of time, and now its my turn to ask a question.

I followed http://net.tutsplus.com/tutorials/php/easy-authentication-with-codeigniter/ tutorial and have managed to get it to work flawlessly for my self, however I need to expand the system ever so slightly. I need to be able to after the user has been verified to pull additional row fields of the authenticated person and store in session variables, for use in other controllers for sending information back to the database.

For instance I want to have in a sessional variable $f_name, and I need their $username. I have looked high and low and have found answers but they have only confused me.

My Model:

class admin_model extends CI_Model {
 function __construct( )
 {

 }

 public function verify_user($user_id, $password)
 {
 $q = $this->db->where('user_id', $user_id)
 ->where('password', sha1($password))->limit(1)->get('users');

 if ( $q->num_rows > 0   ){
     return $q->row();
 }
return false;

}

}

My Controller:

class admin extends CI_Controller {

function __construct()
{
parent:: __construct();
session_start();


}

public function index()
{
    if ( isset($_SESSION['user_id'])){
        redirect('welcome');
    }
    $this->load->library('form_validation');
    $this->form_validation->set_rules('user_id', 'User ID', 'required|min_length[8]');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');

    if ($this->form_validation->run() !==false){
       $this->load->model('admin_model');
      $res = $this
          ->admin_model
          ->verify_user(
          $this->input->post('user_id'),
          $this->input->post('password')
      );
   if ($res !== false) {
    $_SESSION['user_id'] = $this->input->post('user_id');
    redirect ('welcome');
   }






    }

    $this->load->view('login_view');
}

public function logout(){

    session_destroy();
    redirect ('admin');
}
}

Thanks again guys and I look forward to your answers\suggestions

Upvotes: 1

Views: 1521

Answers (1)

user1700924
user1700924

Reputation:

A couple of things:
- the __construct() function in admin_model needs parent::__construct (or the function could be removed since it's empty) to inherit the code igniter model constructor.
- code igniter and PHP sessions are different. You're using PHP sessions, which means that you'll have to call session_start() before any output is sent on every page in which you'd like to access the session data. This can also be set for every page in your php.ini by turning on session.autostart

If you'd like to use code igniter sessions, look here: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

UPDATE 12/21/2012

Try setting the session variables from the database row returned:

$_SESSION['f_name'] = $res->f_name; and $_SESSION['username'] = $res->username; in the same place as where you're setting the session user id variable. I'm assuming your database fields are mapping to the fields that you're wanting to use for the f_name and username fields.

Upvotes: 4

Related Questions