2018-05-20 07:48
浏览 66

如何访问存储在数组codeigniter中的$ sESSION数据

I have been following a tutorial on youtube about CodeIgniter. The tutorial only relies on $username $_SESSION variable and doesn't touch on other aspects of $_SESSIONs. According to code igniter documentation regarding $_SESSION you can simply call the session super global with the key. This is where my problem starts.


a var_dump of $_Session provides the following info (I have truncated other fields to keep it shorter)

 array (size=5)
      '__ci_last_regenerate' => int 1526800056
      '' => null
      'userID' => 
        array (size=1)
          0 => 
            array (size=14)
              'userID' => string '1' (length=1)
              'firstname' => string 'Tim' (length=3)
              'lastname' => string 'Coetzee' (length=7)
              'pword' => string 'xyz' (length=4)
              'email' => string '' (length=17)

What I want to do

I simply want to be able to call a $_SESSION super global as such $_SESSION['email'] or $_SESSION['userID'] The way I want to access the session vars is EXACTLY as you should do it according to docs, thus I believe my problem is in my controller() or the way im setting the session data() as can be viewed below


Lets say I want to display the userID from above var_dump() info in the view just as a test I do the following... echo $_SESSION['userID'];

The above leads to error

Message: Object of class stdClass could not be converted to string

Okay sure enough however what is the object name I should call to get say firstname ?

I tried this as per official docs

echo $this->session->firstname AND echo $this->user_data('userID'); which resulted in same error.

I realize the $_SESSION data seems to be saved in a multi-dimensional array so I tried a foreach() as such

foreach ($_SESSION as $sessions)
    foreach ($sessions as $value)
        echo $value;

which gives me the error:

Message: Invalid argument supplied for foreach()

However after two more similar errors by the last iteration it returns the values of all the sessions, so Im guessing im doing something wrong in the loop.

Code Follows Below


public function login($username, $password)
        // Validate
        $this->db->where('username', $username);
        $this->db->where('pword', $password);
        $stmnt = $this->db->get('users');
        if($stmnt->num_rows() == 1){
            return $stmnt->row(1);
        } else {
            return false;


public function login(){
        $data['title'] = 'Sign In';
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        if($this->form_validation->run() === FALSE){
            $this->load->view('users/login', $data);
        } else {
            // Get username
            echo $username = $this->input->post('username');
            // Get and encrypt the password
            echo $password = $this->input->post('password');
            // Login user
            $user_id = $this->users_model->login($username, $password);
                // Create session
                $user_data = array(
                    'username' => $username,
                    'userID' => $user_id,
                    'logged_in' => true
              $username = user_data ['username'];
              $userID = user_data ['userID'];
              $logged_in = user_data ['logged_in']; 

                $this->session->set_userdata($username );
                $this->session->set_userdata($userID );
                // Set message
                //$this->session->set_flashdata('user_loggedin', 'You are now logged in');
            } else {
                // Set message
               // $this->session->set_flashdata('login_failed', 'Login is invalid');


$autoload['libraries'] = array('form_validation','session', 'pagination');

Any help advice, or constuctive criticism appreciated

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题