1

我已经设置了我的网站,因此只有在用户登录之前才能访问添加和登录。当他登录时,他会被重定向到主页。但是当用户尝试登录时,登录页面会刷新并显示此消息“用户名或密码不正确”,这是来自我的控制器的操作登录。我验证了它是“$this->Auth->login()”,它不起作用。

在我的数据库中,我将这些字段用于用户表:id、password、nom、prenom、username。

这是我的控制器UsersController的登录功能代码:

public function login()
  {
     if ($this->request->is('post'))
     {
        if ($this->Auth->login())
          {
              return $this->redirect($this->Auth->redirectUrl());
          }
         else
         {
              $this->Session->setFlash(__('Username or password is incorrect'));
         }
     }
  }

这是登录视图代码:

<?php
   echo $this->Session->flash('auth');
   echo $this->Form->create('User');
   echo $this->Form->input('username', array('label' => 'Votre pseudo: '));
   echo $this->Form->input('password', array('label' => 'Votre mot de passe: '));
   echo $this->Form->end('Se connecter');
?>

这是用户模型代码:

<?php

   App::uses('AppModel', 'Model');
   App::uses('SimplePasswordHasher', 'Controller/Component/Auth');

   class User extends AppModel
   {
      public $hasMany = array('Compte','ComptesUtilisateur');

      public function beforeSave($options = array()) {
         Security::setHash('md5');

         if(isset($this->data[$this->alias]['password']))
         {
            $passwordHasher= new SimplePasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']);
         }

         return true;
      }
   }
?>

这里是 AppController.php 代码:

class AppController extends Controller {

   var $components = array(
      'Auth' => array(
         'authError' => "Etes-vous sûr(e) d'avoir l'autorisation pour y accéder?",
         'loginError' => "Votre pseudo ou votre mot de passe est incorrect",
         'loginAction' => array(
            'controller' => 'users',
            'action' => 'login'
         ),
         'loginRedirect' => array('controller' => 'pages', 'action' => 'home'),
         'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
         'authenticate' => array(
            'Form' => array(
               'fields' => array(
                  'username' => 'username',
                  'password' => 'password'
               ),
               'passwordhasher' => array(
                  'className' => 'Simple',
                  'hashType' => 'md5'
               )
            )
         )
      ),
      'Session'
   );

   public function beforeFilter()
   {
      $this->Auth->allow('login');
   }

   public function beforeRender()
   {
      $this->set('auth', $this->Auth->loggedIn());
   }
}

我在论坛上搜索了解决方案,但一无所获。

有什么迹象吗?

谢谢,卢伯鲁。

4

0 回答 0