1

我正在尝试在 cakephp 中进行简单的“用户身份验证”。我编写了食谱中给出的代码,但我的登录功能无法正常工作。它不进行任何身份验证,并允许任何用户访问或编辑数据,即使提供空白用户名和密码,也允许我登录。我在 Appcontroller.php 中编写了以下代码

<?php
class AppController extends Controller {

public $components = array(
        'Session',
        'Auth' => array(
         'loginRedirect' => array('controller' => 'users','action' => 'index'),
         'logoutRedirect' => array('controller' => 'users','action' => 'login')
        )
    );
   public function beforeFilter() {
       $this->layout = 'admin_layout';
       $this->Auth->allow('index','view'); 

        }

用户控制器.php

    <?php

    App::uses('AppController', 'Controller');
    App::uses('AuthComponent', 'Controller/Component');

    class UsersController extends AppController {


        public function login() 
            {

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

            }}
            public function logout() {
        $this->redirect($this->Auth->logout());
        }

Login.ctp is as follows



 <?php echo $this->Html->css('login');?>
    <div class="logincontainer">

    <div class="loginform">
    <div class="img">
    </div>


    <?php echo $this->Form->create('User'); ?>
    <div id="input">
    <div id="username">
      <?php // echo $this->Form->input('username');?>

     <?php echo $this->Form->input('username',array('class'=>'name','placeholder'=>'username','label'=>false));?>
    </div>
    <div id="space">
    </div>
    <div id="password">
        <?php //     echo $this->Form->input('password');?> 
    <?php echo $this->Form->input('password',array('class'=>'name','placeholder'=>'password','label'=>false));?>
    </div>
        <div id="space1"></div>
    <?php echo $this->Form->end('Login'); ?>

        <div id="forgot">Forgot Password

            </div>
    </div>
        <?php // echo $this->Session->flash();?>
    </div>
    </div>

有人可以指导我错在哪里..

4

1 回答 1

0

我认为您没有登录而是重定向到索引操作,并且您认为登录是因为您的 loginRedirect 选项是索引操作,并且您在 beforeFilter 中允许此操作

$this->Auth->allow('index','view');

请加:

debug($this->Auth->login());
debug($this->request->data);
die();

前:

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

调试结果($this->Auth->login()); 如果您输入错误的用户名或密码,则应为 false

于 2014-08-31T10:01:46.390 回答