2017-02-01 100 views
1

我有我的表單加載Auth控制器類的提交登錄方法,我有AuthData模型,它具有身份驗證功能。 現在當用戶提交表單時,我得到錯誤?CodeIgniter錯誤 - 調用成員函數null

警告是 -

一個PHP錯誤遇到
嚴重性:注意
消息:未定義的屬性:驗證:: $的authData
文件名:控制器/ Auth.php行號:21

Backtrace:
File:C:\ xampp \ htdocs \ trials \ application \ controllers \ Auth.php Line:21 Function:_error_handler
File:C:\ xampp \ h tdocs \試驗\ index.php的線:315 功能:require_once

錯誤時遇到未捕獲的異常
類型:錯誤
消息:上的空調用一個成員函數的authenticate()
文件名:C: \ XAMPP \ htdocs中\試驗\應用\控制器\ Auth.php行號:21

回溯:
文件:C:\ XAMPP \ htdocs中\試驗\ 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。

+0

您將模型加載到索引上,但不能登錄。如果您希望該模型可用於Auth.php中的所有函數,請創建一個__construct()函數並將其加載到那裏。 – aynber

+0

自動加載數據庫我也會在config/autoload.php中執行無需關閉模型和控制器等''>'codeigniter – user4419336

+0

也可以通過這裏查看https://www.codeigniter.com/user_guide/libraries/input.html和https://www.codeigniter.com/user_guide/libraries/sessions.html – user4419336

回答

0

由於@aynber正確指出AuthData模型在此函數中不可用。

你兩個選擇,其一是:1 - 自動加載它在config/autoload.php腳本@ wolfgang1983狀態或2:設置一個構造函數

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

然後它會在你的控制是可用的。

+0

這是一個錯誤,這整個時間我很明顯,我已經在我的構造函數中加載AuthData ..不管怎麼說,謝謝指出。 –

相關問題