2017-02-01 4 views
1

Ich habe mein Formular, das Login-Methode der Auth-Controller-Klasse beim Senden lädt, und ich habe AuthData-Modell, das eine Authentifizierungsfunktion hat. Jetzt, wenn Benutzer Formular sendet, erhalte ich einen Fehler?CodeIgniter Fehler - Aufruf an eine Member-Funktion auf null

Warnung ist-

Ein PHP-Fehler aufgetreten
Schweregrad: Hinweis
Nachricht: Nicht definierte Eigenschaft: Auth :: $ AuthData
Dateiname: Controller/Auth.php Zeilennummer: 21

Backtrace:
Datei: C: \ xampp \ htdocs \ Studien \ application \ Controller \ Auth.php Line: 21 Funktion: _error_handler
Datei: C: \ xampp \ h tdocs \ Studien \ index.php Line: 315 Funktion: require_once

Fehler Eine abgefangene Ausnahme aufgetreten
Typ: Fehler
Nachricht: Rufen Sie auf eine Elementfunktion authenticate() auf null
Dateiname: C: \ xampp \ htdocs \ Studien \ application \ Controller \ Auth.php Zeilennummer: 21

Backtrace:
Datei: C: \ xampp \ htdocs \ Studien \ index.php Line: 315 Funktion: require_once

Hier ist mein 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'); 
    } 
} 

Mein Modell

<?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; 
    } 
} 
?> 

Und Form_Open im Blick

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

Auch wenn es darauf ankommt, ich index.php entfernt haben.

+0

Sie laden das Modell auf Index, aber nicht einloggen. Wenn dieses Modell für alle Funktionen in Auth.php verfügbar sein soll, erstellen Sie eine __construct() - Funktion und laden Sie sie dort. – aynber

+0

Autoload-Datenbank würde ich auch in config/autoload.php machen Keine Notwendigkeit, Modelle und Controller usw. zu schließen??> 'In codeigniter – user4419336

+0

Schauen Sie auch hier https://www.codeigniter.com/user_guide/libraries/input.html und https://www.codeigniter.com/user_guide/libraries/sessions.html – user4419336

Antwort

0

Wie @aynber korrekt angibt, ist das AuthData-Modell in dieser Funktion nicht global verfügbar.

Ihre 2 Optionen sind entweder: 1 - automatisches Laden in der config/autoload.php Skript als @ wolfgang1983 Staaten oder 2: setup ein Konstruktor

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

Es wird dann in Ihrem Controller zur Verfügung.

+0

Es ist ein Fehler, die ganze Zeit Ich war offensichtlich, dass ich AuthData in meinem Konstruktor geladen habe. Anyways danke für das Aufzeigen. –

Verwandte Themen