2014-04-07 55 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版本的错误