会话用户数据出错

时间:2014-11-28 09:55:58

标签: php codeigniter session login

这是我的控制器:

<?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);时,它可以正常工作,但上面的代码却没有。有人能告诉我,问题在哪里吗?

3 个答案:

答案 0 :(得分:0)

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

这可能是您的代码无效的原因。

答案 1 :(得分:0)

在codeigniter设置会话值的方法是

$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"));

请查看codeigniter documentation

答案 2 :(得分:0)

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

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