3

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 Answers 3

6

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');
Sign up to request clarification or add additional context in comments.

1 Comment

My code is work with single data method as i mentioned above, and i've tried your array suggestions but still didn't work.
1

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');
    }
}

2 Comments

Oh i've solved it. I think i was wrote a wrong code, so here is the work code: Dashboard Controller public function __construct() { parent::__construct(); if(!$this->session->userdata('log')) { redirect('login'); } } public function index() { $username = $this->session->userdata('log'); //Pass it in an array to your view like $data['username']=$username['username']; $this->load->view('Dashboard', $data); }
did my answer help you?
0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.