我在codeigniter中创建了一个登录系统。我是codeigniter的新手。我在测试登录系统时遇到此错误
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/login.php
Line Number: 92
Fatal error: Call to a member function userdata() on a non-object in P:\xampp\htdocs\xxxxx\xxxxx\application\controllers\login.php on line 92
这是我的login.php控制器文件
<?php
class Login extends CI_Controller {
function __construct()
{
if($this->is_logged_in() === true){
redirect('welcome');
}
}
function index()
{
$data['main_content'] = 'login_form';
$data['class_name'] = get_class();
$data['no_header'] = '1';
$data['no_footer'] = '1';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('admin_model');
$query = $this->admin_model->validate();
print_r($query);
if($query) // if the user's credentials validated...
{
$data = array(
'AdminID' => $query,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('welcome');
}
else // incorrect username or password
{
echo "here";
exit;
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
function is_logged_in()
{
$CI =&get_instance();
$is_logged_in = $CI->session->userdata('is_logged_in');
if(isset($is_logged_in) || $is_logged_in === true)
{
return true;
}else{
return false;
}
}
}
我试过了
$CI =&get_instance();
$CI =get_instance();
$this->session->userdata
所有这三种方式都会出错。
答案 0 :(得分:3)
您正在调用Controller的构造函数中的is_logged_in()
函数,而不首先调用父构造函数。也许这可能是为什么有些资源尚未加载的原因。
尝试添加:
function __construct()
{
parent::__construct(); //you always have to call parent's constructor before ANYTHING
if($this->is_logged_in() === true){
redirect('welcome');
}
}
答案 1 :(得分:0)
您没有为使用get_instance()创建的 new 实例加载会话库。
简单地说,假设会话已经加载,并且如@jere所述,你在控制器中调用parent :: __ construct():
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(isset($is_logged_in) || $is_logged_in === true)
{
return true;
}else{
return false;
}
}
不知道为什么在这里使用该类的另一个实例。
如果您这样做,请尝试再次加载会话库,例如:
$CI->load->library('session')
$is_logged_in = $CI->session->userdata('is_logged_in');
//rest of your old code