3
votes

I'm not quite sure why the username didn't appear on view page if i change the session data into array (i'm new in CodeIgniter). I have auth controller to make login process:

function index() {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean');
            $this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('Login');
            # code...
        }
        else {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $check = $this->M_login->check($username, $password);

            // session data
            if ($check->num_rows() == TRUE) {
                foreach ($check->result() as $value) {
                    $sess_data['id']            = $value->id;
                    $sess_data['name']          = $value->name;
                    $sess_data['username']      = $value->username;
                    $sess_data['password']      = $value->password;
                    $sess_data['description']   = $value->description;
                    $this->session->set_userdata($sess_data);                        
            }
            redirect('Dashboard');
        }
        else {
            $this->session->set_flashdata('result_login', '<br>Invalid username or password, try again.');
            redirect('Login');
        }

And here is my dashboard controller:

public function __construct() {
            parent::__construct();
            if(!$this->session->userdata('id')){
                redirect('login');
            }
    }

    public function index() {
        // Dashboard view
        $username = $this->session->userdata('username');

        $data['username']=$username;

        $this->load->view('Dashboard', $data);    
    }

    function logout() {
        $this->session->sess_destroy('id');
        redirect('login');
    }

With above code, i can get the username on my dashboard view by echo $username. But when i change the session data like this:

if ($check->num_rows() == TRUE) {
   foreach ($check->result() as $value) {

   // session data
   $sess_data = array(
               'id'           => $value->id,
               'username'     => $value->username,
               'password'     => $value->password,
               'name'         => $value->name,
               'description'  => $value->description
             );
   $this->session->set_userdata('log',$sess_data);
   }
}

And Dashboard controller changed like this:

if(!$this->session->userdata('id')) {
     $getdata= $this->session->userdata('log');

     $data['username'] = $getdata['username'];
}
$this->load->view('Dashboard', $data);

Then the username disappeared from view page. How can i store username in session array and call it in view page. Please share your better suggestions or your experience guys.

Thanks.

3

3 Answers

5
votes

Adding data in session :-

$newdata = array(
               'username'  => 'johndoe',
               'email'     => '[email protected]',
               'logged_in' => TRUE
           );

$this->session->set_userdata($newdata);

Retrieving Data from session :-

$this->session->all_userdata()

Retrieving single data :-

$session_id = $this->session->userdata('username');
1
votes

Youcan refer this code

public function loginaction()
{
    $this->load->library('session');
    $a      =   $this->input->post('email');
    $b      =   trim($this->input->post('password'));
    $b1     =   md5($b);
    $c      =   1;
    $data   =   $this->userdata->userlogin($a,$b1,$c);
    if($data)
    {
        echo true;
        foreach ($data as $login)
        {
            $uid=$this->session->set_userdata('logId',$login->usr_id);
        }
    }
    else
    {
        echo "Invalid username or password..!!!!";
        $a=$this->input->post('email');
    }
}
0
votes

You can do it like

$username   = $this->input->post('username');
$password   = $this->input->post('password');
$check      = $this->M_login->check($username, $password);

if ($check->num_rows() > 0) 
{
    $value = $check->row();
    $sess_data['id']            = $value->id;
    $sess_data['name']          = $value->name;
    $sess_data['username']      = $value->username;
    $sess_data['password']      = $value->password;
    $sess_data['description']   = $value->description;
    $this->session->set_userdata($sess_data);   
    redirect('Dashboard');
}
else
{
    $this->session->set_flashdata('result_login', '<br>Invalid username or password, try again.');
    redirect('Login');
}

And in your view

$username = $this->session->userdata('username');