2014-04-07 102 views
0

夥計們我上傳我的網站在服務器上,我正面臨着這個問題。Cakephp身份驗證不生產

Usercontroller.php

<?php 
class UsersController extends AppController { 

    var $name = 'Users'; 
    var $components = array('Auth', 'Acl'); 


    function beforeFilter(){ 
     //parent::beforeFilter(); 
     $this->Auth->loginError = "asdad!"; 
     $this->Auth->authError = "sdsd !."; 
     App::uses('CakeEmail', 'Network/Email'); 
     $this->Auth->userModel = 'User'; 
     $this->Auth->allow('login','register','login','step2','TakeId','kontakt'); 
    } 
    function login(){ 
     if(!empty($this->data)){ 
      $this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']); 
      $sprawdz = $this->User->find('first',array('conditions'=>array('User.username'=>$this->request->data['User']['username'],'User.password'=>$this->request->data['User']['password']))); 
      echo debug($sprawdz); 
      if(!empty($sprawdz)){ 
       if($this->Auth->login($this->request->data)){ 
        $this->redirect('/'); 
        echo debug('you are log in!') ; 
        } 
      } 
      else { 
       $this->User->invalidate('username', 'mistake!'); 
      } 
     } 
    } 

    function logout(){ 
     $this->Auth->logout(); 
     $this->redirect('/'); 
    } 
} 

用戶/ login.ctp

<?php echo $this->Session->flash('auth'); ?> 
<?php echo $this->Form->create('User'); ?> 
    <fieldset> 
     <?php echo $this->Form->input('username'); 
       echo $this->Form->input('password'); ?> 
    </fieldset> 
<?php echo $this->Form->end(__('Log in')); ?> 

它就像驗證沒有在所有的工作。在localhost很好。另外,users/index是空白頁:)

任何想法都會很棒。謝謝。

+0

?總是試圖在你的問題上表明這一點。 –

+0

可能重複的[CakePHP:不能做簡單的登錄](http://stackoverflow.com/questions/22748215/cakephp-cant-do-simple-login) – Fury

回答

0

上登錄的2號線加$這個 - >請求 - >數據aciton-

function login(){ 
    if(!empty($this->request->data)){ 
     $this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']); 
     $sprawdz = $this->User->find('first',array('conditions'=>array('User.username'=>$this->request->data['User']['username'],'User.password'=>$this->request->data['User']['password']))); 
     echo debug($sprawdz); 
     if(!empty($sprawdz)){ 
      if($this->Auth->login()){ 
       $this->redirect('/'); 
       echo debug('you are log in!') ; 
       } 
     } 
     else { 
      $this->Session->setFlash('Invalid username or password'); 
     } 
    } 
} 
+0

我使用CakePhp 2.0; P,無論如何,我有很好的問到SQL ,但驗證 - >登錄不起作用 – user2761717

+0

你的代碼是完全混亂的。註冊一個新用戶時如何哈希密碼?爲什麼在登錄操作中使用查找條件,而Auth組件可以處理所有這些事情....? –

+0

@ user2761717它「不起作用」,因爲您手動查找用戶,銷燬請求數據,然後要求auth組件使用(銷燬的)請求數據進行登錄; auth組件將再次散列密碼。 – AD7six

1

保持簡單和follow the tutorial它展示瞭如何使用身份驗證的組件。

例如,試試這個:

function login(){ 
    if ($this->request->is('post')) {  
      if ($this->Auth->login()) { 
       $this->Session->setFlash('You have successfully logged in the system'); 
       return $this->redirect('/'); 
      } 
      $this->Session->setFlash(__('Invalid username or password, try again [warning]')); 
     } 
} 

同時請確保Security.Salt負責與

你沒有定義在usersController任何索引功能的應用程序的鹽值進行散列密碼,那就是爲什麼它會拋出錯誤。嘗試啓用調試模式,以便您可以跟蹤您的cakephp版本的錯誤