【问题标题】:cakephp redirect querycakephp重定向查询
【发布时间】:2012-07-25 05:23:28
【问题描述】:

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

当我们登录站点时,在检查站点正在检查数据库的 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');

     ?> 

【问题讨论】:

  • 移除硬编码条件并检查 - if ($this->request->data['User']['password'] == 'qazwsx'){ }
  • 我删除了 if 语句,现在返回无效的用户名/密码

标签: cakephp redirect login


【解决方案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.');
      }
  }

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 将调试从 3 更改为 2,密码现在是 varchar(255)。我检查了网站发送的密码和数据库中的密码(两者都是加密的),它们是相同的。它仍然抛出错误
    • 嘿,谢谢,我们已经解决了我们的问题。哈希密码长度比密码列长度长,因此我们尝试登录时密码不同。
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      相关资源
      最近更新 更多