简体   繁体   中英

PHP CodeIgniter session information lost

Here is the code where session array is being set:

function login($username,$password)
 {

  $this->db->select('user_id', 'username', 'password', 'email', 'register_date', 'permissions');
  $this->db->from('users');
  $this->db->where("username",$username);
  $this->db->where("password",sha1(md5($password)));

  $query=$this->db->get();

  if($query->num_rows() == 1)
  {
   foreach($query->result() as $rows)
   {
     $newdata = array(

      'user_name'  => $rows->username,
      'user_id'    => $rows->user_id,
      'user_email' => $rows->email,
      'user_date'  => $rows->register_date,
      'user_perm'  => $rows->permissions,
      'logged_in'  => TRUE,
    );

   }
   $this->session->set_userdata($newdata);
   return true;
  }
  return false;
 }

And here is the result of

<?php print_r ( $this->session->all_userdata()); ?>

Array
(
    [session_id] => d18899b96fd0cd31c8e2f64cac152133
    [ip_address] => ::1
    [user_agent] => Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36
    [last_activity] => 1399503084
    [user_data] => 
    [user_name] => 
    [user_id] => 24
    [user_email] => 
    [user_date] => 
    [user_perm] => 
    [logged_in] => 1
)

As you can see, only user_id session is good, everything else has null value, do you know what the problem is?

Database: http://prntscr.com/3h6fc9

EDIT: SOLVED! The issue was in login function, i use this instead:

function login($username,$password)
 {


  $password = sha1(md5($password));
  $query = $this->db->get_where('users', array('username' => $username, 'password' => $password));

  if($query->num_rows() == 1)
  {
   foreach($query->result() as $rows)
   {
     $newdata = array(

      'user_name'  => $rows->username,
      'user_id' => $rows->user_id,
      'user_email'    => $rows->email,
      'user_date'    => $rows->register_date,
      'user_perm'    => $rows->permissions,
      'logged_in'  => TRUE
    );

   }
   $this->session->set_userdata($newdata);
   return true;
  }
  return false;
 }

in your model you just return $newdata instead of true AND in your controller get the return data in variabl $aaa. then in next line set session

 $this->session->set_userdata('logged_info',  $aaa);

now your session is set. You can get that any where like it

$data = $this->session->userdata('logged_info')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM