在非对象上调用成员函数set_userdata()

时间:2015-02-28 18:06:46

标签: php validation codeigniter login codeigniter-form-helper

我在第16行收到以下错误

我在autoload上有会话库

控制器登录

class Login extends CI_Controller {

function __construct() { parent::__construct(); }

function index() { $this->load->helper(array('form')); $this->load->view('login_view'); }

}

?> ;

controller verifylogin

class VerifyLogin extends CI_Controller {

function __construct() { parent::__construct(); $this->load->model('user','',TRUE); }

function index() { //This method will have the credentials validation $this->load->library('form_validation');

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules ('password', 'Password', 'trim|required|xss_clean|callback_check_database');

if($this->form_validation->run() == FALSE) { //Field validation failed. User redirected to login page $this->load->view('login_view'); } else { //Go to private area redirect('home', 'refresh'); }

}

function check_database($password) { //Field validation succeeded. Validate against database $username = $this->input->post('username');

//query the database $result = $this->user->login($username, $password);

if($result) { $sess_array = array(); foreach($result as $row) { $sess_array = array( 'id' => $row->id, 'username' => $row->username ); $this->session->set_userdata('logged_in', $sess_array); } return TRUE; } else { $this->form_validation->set_message('check_database', 'Invalid username or password'); return false; } } } ?> ;

模型用户

db -> select('id, username, password'); $this -> db -> from('users'); $this -> db -> where('username', $username); $this -> db -> where('password', MD5($password)); $this -> db -> limit(1);

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

if($query -> num_rows() == 1) { return $query->result(); } else { return false; } } } ?> ;

1 个答案:

答案 0 :(得分:0)

此错误表示会话成员未设置为verifyLogin实例上的对象。

确保已正确分配$ this->会话。