我的表单可以在提交时加载Auth控制器类的登录方法,而我的AuthData模型具有身份验证功能。现在,当用户提交表单时,我得到了错误?
警告是-
遇到PHP错误
严重性:通知
消息:未定义的属性:Auth :: $ AuthData
文件名:controllers / Auth.php行号:21回溯:
文件:C:\ xampp \ htdocs \ trials \ application \ controllers \ Auth.php行:21函数:_error_handler
文件:C:\ xampp \ htdocs \ trials \ index.php行:315函数:require_once错误遇到未捕获的异常
类型:错误
消息:在null上调用成员函数authenticate()
文件名:C:\ xampp \ htdocs \ trials \ application \ controllers \ Auth.php行号:21回溯:
文件:C:\ xampp \ htdocs \ trials \ index.php行:315功能:require_once
这是我的控制器
<?php
//defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
$this->load->model('AuthData');
$this->load->helper('url');
// needed ???
$this->load->database();
}
public function login()
{
$user = $this->input->post('username');
$pass = $this->input->post('password');
$input = array( 'username'=>$user, 'password'=>$pass);
$data['loggedIn'] = "no";
$chk = $this->AuthData->authenticate($input);
if($chk)
redirect('Sample/success');
else
redirect('Sample/failed');
//$this->load->view('home/contact');
}
}
我的模特
<?php
//session_start();
class AuthData extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function authenticate($input=NULL)
{
$query = $this->db->query('SELECT * FROM user');
$rows = $query->result_array();
foreach($rows as $check)
{
if($input['password']==$check['password'])
{
if($input['username']==$check['usernmae'])
{
//session_start();
$_SESSION['login'] = "T";
$_SESSION['username'] = $input['username'];
//is it unsafe to keep password?
$_SESSION['password'] = $input['password'];
return true;
//maybe break here is redundant,but dont want risk
break;
}
}
}
return false;
}
}
?>
和form_open在视图中
<?php echo form_open('auth/login', ['class' => 'form-signin', 'role' => 'form']); ?>
另外,如果有关系,我已经删除了index.php。
正如@aynber正确指出的那样,该函数无法全局使用AuthData模型。
您的2种选择是:1-按照@ wolfgang1983的说明在config / autoload.php脚本中自动加载,或者2:设置构造函数
public function __construct(){
$this->load->model('Authdata');
}
然后,它将在整个控制器中可用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句