会话用户数据出错



这是我的控制器:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class admin extends ci_controller{

function __construct(){
    parent::__construct();
    $this->load->library('session');
    $this->load->helper(array('form', 'url', 'security', 'html'));
    $this->load->library('form_validation');
    $this->load->model($this->config->item('admin_model'), 'admin_db_connection', TRUE);
    //$this->load->model('admin_model' ,TRUE);
    //$this->load->helper('security');
    $this->load->helper('date');
    //$this->load->library('Ajax');
    $this->load->library("pagination");
    $this->load->database();
    $this->load->helper('url'); //You should autoload this one ;)
}

public function index(){
    $this->load->view('admin/login');
}

public function check_admin(){ //echo'admin';
    $this->load->helper('form');
    $this->form_validation->set_rules('admin_name','admin name','trim|required|xss_clean');
    $this->form_validation->set_rules('email','Email','trim|required|xss_clean');
    $this->form_validation->set_rules('password','password','trim|required|xss_clean');
    if($this->form_validation->run()== FALSE)
    { //echo 'invalid login ';
        $this->index();
    }
    else
    {
        $data=array();
        $data['admin_name']=$this->input->post('admin_name',true);
        $data['email']=$this->input->post('email',true);
        $data['password']=$this->input->post('password',true);
        //echo "<pre>";
        if ($query = $this->admin_db_connection->check_admin_validation($data))
        {
            // print_r($po);exit;
            $newdata=array
            (
             'id' => $query[0]['id'],
             'admin_name' => $query[0]['admin_name'],
             'email'=>$query[0]['email'],
             'status' => $query[0]['status'],
             'sign_in'=>TRUE
             );
            // echo "<pre>";
            //print_r($newdata);exit;
            $this->session->set_userdata('auth', $newdata);
            //$return= true;
            //print_r($auth);exit;
            redirect('dashboard');
            return TRUE;
        }
        else
        {
            echo 'invalid login';
            $this->index();
            //redirect('admin/index', 'refresh');
            return FALSE;
        }
    }
}
  function logout()
  {
    $this->session->sess_destroy();
    redirect('admin');
  }
}
?>

这是我的模型:

 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class admin_model extends ci_model{

function check_admin_validation($data){
    $this->session->userdata('auth');
    $this->db->select('*');
    $this->db->from('admin_user');
    $this->db->where('admin_name' , $data['admin_name']);
    $this->db->where('email', $data['email']);
    $this->db->where('password', $data['password']);
    $query=$this->db->get();
    if($query->num_rows()>0)
    {
        $resulet=$query->result_array();
        $query->free_result();
        return $resulet;
    }
    else        
    {
        return false;   
    }
  }
}
?>

当我$this->session->set_userdata($newdata);而不是$this->session->set_userdata('auth', $newdata);使用此代码时,它可以工作,但上面的代码不起作用。有人可以告诉我,问题出在哪里吗?

如果我

没记错的话,set_userdata函数的第二个参数是一个字符串,而你传递的是一个数组。

这可能是您的代码无法正常工作的原因。

在代码点火器上设置会话值的方法是

$this->session->set_userdata('some_name', 'some_value');
//some_value should not be array
//But your are setting array as value which is wrong.
$this->session->set_userdata($newdata); 
//This will set each array key to your session.
//That's why it is working

如果您真的想将数组设置为一个 sesson 键,您可以按照以下方式进行操作

   $this->session->set_userdata("auth",json_encode($newdata);

现在,您可以将数据检索为

$auth=json_decode($this->session->userdata("auth"));

请查看代码点火器文档

谢谢你现在我解决了我的问题。谢谢大家和 stackoverflow,因为如果我不在这里发帖,我就找不到我的答案。这是我的问题 仪表板 我传递了错误的值 'sing_in' 正确的值 'auth'.

 if (!$this->session->userdata('sing_in'))
          {  
              redirect('admin');
          }`enter code here`

相关内容

  • 没有找到相关文章