2013-05-16 60 views
0

登錄功能在PHP的蛋糕工作如何在cake php中登錄函數?

我產生UsersController.php

 public function login() { 
     if ($this->Auth->login()) { 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
    } 

    public function add() { 
     if ($this->request->is('post')) { 
     $this->User->create(); 
     $this->request->data['User']['password'] = md5($this->request->data['User']['password']); 
     if ($this->User->save($this->request->data)) { 
      $this->Session->setFlash(__('The user has been saved')); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
     } 
    } 
    } 

我的模型文件Users.php

  <?php 
      App::uses('AuthComponent', 'Controller/Component'); 

      class User extends AppModel { 
       public $validate = array(
        'username' => array(
        'required' => array(
         'rule' => array('notEmpty'), 
         'message' => 'A username is required' 
        ) 
         ), 
         'password' => array(
         'required' => array(
          'rule' => array('notEmpty'), 
         'message' => 'A password is required' 
        ) 
         ), 
        'role' => array(
         'valid' => array(
          'rule' => array('inList', array('admin', 'author')), 
            'message' => 'Please enter a valid role', 
          'allowEmpty' => false 
         ) 
        ) 
       ); 

        public function beforeSave($options = array()) 
        { 
         if (isset($this->data[$this->alias]['password'])) 
         { 
         $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
        } 

         return true; 
        } 
       } 
       ?> 

我的看法文件是Uesrs/login.ctp

  <div class="users form"> 

       <?php echo $this->Session->flash('auth'); ?> 
       <?php echo $this->Form->create('User'); ?> 

       <fieldset> 

        <legend><?php echo __('Please enter your username and password'); ?></legend> 
        <?php echo $this->Form->input('username'); 
        echo $this->Form->input('password'); 
        ?> 

       </fieldset> 

       <?php echo $this->Form->end(__('Login')); ?> 

      </div> 

當我運行這段代碼時,它告訴「無效的使用rname或密碼,再試一次「。

但我輸入正確的用戶名和密碼。

爲什麼會出現這個問題?

請幫幫我。我是Cake Php的新成員。

+0

請問您可以發佈您的完整代碼嗎? – Matheno

回答

1

試試這個。 你確定要輸入用戶名和密碼嗎?可能在數據庫中再次檢查它?

public function login() 
     { 
     if($this->request->is('post')) 
     { 
     App::Import('Utility', 'Validation'); 
     if(isset($this->data['User']['username']) && Validation::email($this->data['User']['username'])) 
     { 
      $this->request->data['User']['email'] = $this->data['User']['username']; 
      $this->Auth->authenticate['Form'] = array('fields' => array('username' => 'email')); 
     } 

     if(! $this->Auth->login()) 
     { 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
     else 
     { 
      $this->redirect($this->Auth->redirect()); 
     } 
     } 
    } 
+0

用戶名和密碼在數據庫中正確。但是當我們通過登錄傳遞數據的時候,密碼顯示在加密中,並且數據庫在加密時也保存了密碼。但他們是不同的。爲什麼兩個密碼都顯示不同的答案。我只使用用戶名和密碼。 – krutssss

+0

當它們不同時加密;那麼你沒有使用正確的密碼。 – Matheno