3

创建一个基本的登录页面 - 它正在工作,但后来我们更改了我们的数据库,我们的重定向不再工作。

当我们登录站点时,在检查站点正在检查数据库的 sql 代码后,返回说用户名/密码不正确 - 它正在发送正确的信息,但不允许我们登录。

我们希望用户在登录站点时被重定向到 ebox(控制器)主页(视图)。

这是控制器中用于登录的代码

    public function login(){

    $this->set('title_for_layout', 'Individual Registration');
    $this->set('stylesheet_used', 'style');
    $this->set('image_used', 'eBOXLogo.jpg');

    if ($this->request->is('post')){
        if ($this->Auth->login()){
            $username = $this->request->data['User']['username'];
            if (0 === $this->User->find('count',array('conditions'=>array('activated'=>true,'username'=> $username)))){
                $this->Session->setFlash('Sorry, your account is not validated yet.');
                $this->redirect($this->referer());
                } 
            else{
                $this->Auth->user('id');
                $this->redirect( array('controller' => 'Eboxs','action' => 'home'));
                }
        }  

        else{
            $this->Session->setFlash('Username or password is incorrect');
        }
    }else{
        $this->Session->setFlash('Welcome, please login');
    }


}

这是视图的代码

<?php    
         echo $this->Form->create('User', array('action' => 'login'));
         echo $this->Form->input('username');
         echo $this->Form->input('password');
         echo $this->Form->end('Login');

     ?> 
4

3 回答 3

1
I think try this

//app controller
class AppController extends Controller {

    public $components = array(
        'Acl',
        'Auth' => array(
            'authorize' => array(
                'Actions' => array('actionPath' => 'controllers')
            )
        ),
        'Session'
    );


    public function beforeFilter() {

     //Configure AuthComponent
     $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
     $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
     $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
    }

}


//login action view

echo $this->Form->inputs(array(
    'legend' => __('Login'),
    'username',
    'password'
));


//this is controller code


 if ($this->request->is('post')) {
      if ($this->Auth->login()) {
   $this->redirect($this->Auth->redirect());
      } else {
   $this->Session->setFlash('Your username or password was incorrect.');
      }
  }
于 2012-07-25T11:01:42.907 回答
0

当您更改数据库时,在 core.php 文件中将调试模式启用为“2”,或者您可以删除模型缓存。您可以稍后在生产站点中将调试模式更改为 0。还要检查新数据库的用户表中的用户名和密码是否有效。还要检查密码字段的结构。它应该是 varchar 255。

还将上述逻辑修改为 -

if ($this->Auth->login()){
  $username = $this->request->data['User']['username'];
  if (0 === $this->User->find('count',array('conditions'=>array('activated'=>true,'username'=> $username)))) {

     $this->Session->setFlash('Sorry, your account is not validated yet.');
     $this->redirect($this->referer());
   } else {
    $this->Auth->user('id');
    $this->redirect( array('controller' => 'Eboxs','action' => 'home'));
   }   
} 

在视图文件中而不是使用

echo $this->Form->create('User', array('action' => 'login'));

尝试使用 -

echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'login')));
于 2012-07-25T06:26:05.390 回答
0

在以下情况下,您可以检查代码不工作的原因:

  1. 您是否在用户模型中使用了 beforeFilter() 方法?

    如果是,请检查它在说什么。

  2. 将您的“登录”方法放入用户控制器的 beforeFilter 中的 Auth->allow() 方法中。

    function beforeFilter(){
        $this->Auth->allow('login');
    }
    
  3. 请检查保存到数据库中的相关哈希密码是否等于您为相应用户输入的密码。您可以使用以下语法检查 Auth 哈希密码:

    pr(AuthComponent::password('USER PASSWORD HERE'));
    
  4. 您可以使用以下语法来创建表单:

    echo $this->Form->create('User', array('url' => $this->request->params));
    

请询问它是否仍然不适合您。

于 2012-07-25T09:52:35.220 回答