gpt4 book ai didi

php - CodeIgniter 错误 - 在 null 上调用成员函数

转载 作者:行者123 更新时间:2023-12-02 00:48:24 26 4
gpt4 key购买 nike

我的表单在提交时加载了 Auth Controller 类的登录方法,并且我的 AuthData 模型具有验证功能。现在当用户提交表单时出现错误?

警告是-

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$AuthData
Filename: controllers/Auth.php Line Number: 21

Backtrace:
File: C:\xampp\htdocs\trials\application\controllers\Auth.php Line: 21 Function: _error_handler
File: C:\xampp\htdocs\trials\index.php Line: 315 Function: require_once

Error An uncaught Exception was encountered
Type: Error
Message: Call to a member function authenticate() on null
Filename: C:\xampp\htdocs\trials\application\controllers\Auth.php Line Number: 21

Backtrace:
File: C:\xampp\htdocs\trials\index.php Line: 315 Function: require_once

这是我的 Controller

<?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 在 View 中

<?php echo form_open('auth/login', ['class' => 'form-signin', 'role' => 'form']); ?>

此外,如果重要的话,我已经删除了 index.php。

最佳答案

正如@aynber 正确指出的那样,AuthData 模型在此函数中不可全局使用。

您的 2 个选项是:1 - 在 config/autoload.php 脚本中自动加载它作为 @wolfgang1983 状态或 2:设置构造函数

public function __construct(){
$this->load->model('Authdata');
}

然后它将在整个 Controller 中可用。

关于php - CodeIgniter 错误 - 在 null 上调用成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41988086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com