cakephp重定向查询

cakephp重定向查询,cakephp,redirect,login,Cakephp,Redirect,Login,创建一个基本的登录页面-它可以正常工作,但后来我们更改了数据库,重定向不再工作 当我们登录时,在检查sql代码后,站点返回说用户名/密码不正确,该站点正在检查数据库-它正在发送正确的信息,但不允许我们登录 我们希望用户登录站点时能够重定向到eboxs(控制器)主页(视图) 以下是控制器中用于登录的代码 public function login(){ $this->set('title_for_layout', 'Individual Registration');

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

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

我们希望用户登录站点时能够重定向到eboxs(控制器)主页(视图)

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

    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');

     ?> 

更改数据库时,在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')));

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

  • 您在用户模型中使用过beforeFilter()方法吗

    如果是,那么检查它在说什么

  • 将“login”方法放入UsersController的beforeFilter中的Auth->allow()方法中

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

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

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

  • 请询问它是否仍然对您无效。

    删除硬编码条件并检查-if($this->request->data['User']['password']=='qazwsx'){}我删除了if语句,现在返回无效的username/password将调试从3更改为2,密码现在为varchar(255)。我已经检查了站点发送的密码和数据库中的密码(都是加密的),它们是相同的。它仍然在抛出一个错误嘿,谢谢,我们已经解决了我们的问题。哈希密码长度比密码列长度长,因此在尝试登录时密码不同。
    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.');
          }
      }