【发布时间】:2017-06-18 16:34:06
【问题描述】:
我的表单在提交时会加载 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。
【问题讨论】:
-
您在索引上加载模型,但没有登录。如果您希望该模型可用于 Auth.php 中的所有函数,请创建一个 __construct() 函数并将其加载到那里。
-
自动加载数据库我也会在 config/autoload.php 中执行 无需在 codeigniter 中关闭模型和控制器等
?>
标签: php codeigniter null