Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP 2.4认证->;login()始终返回FALSE_Php_Cakephp_Login - Fatal编程技术网

CakePHP 2.4认证->;login()始终返回FALSE

CakePHP 2.4认证->;login()始终返回FALSE,php,cakephp,login,Php,Cakephp,Login,我正在为我妈妈开发一个带有cake 2.4的简单登录应用程序。下面是用户模型的代码 App::uses('SimplePasswordHasher', 'Controller/Component/Auth'); class User extends AppModel{ public $validate = array( 'username' => array( 'required' =>

我正在为我妈妈开发一个带有cake 2.4的简单登录应用程序。下面是用户模型的代码

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

class User extends AppModel{
        public $validate = array(
                'username' => array(
                        'required' => array(
                                'rule' => array('notEmpty'),
                                'message' => 'A username is required'
                        )
                ),
                'password' => array(
                        'required' => array(
                                'rule' => array('notEmpty'),
                                'message' => 'A password is required'
                        )
                )
        );

        public function beforeSave($options = array()) {
                if(isset($this->data[$this->alias]['password'])) {
                        $passwordHasher = new SimplePasswordHasher();
                        $this->data[$this->alias]['password'] = $passwordHasher->hash(
                        $this->data[$this->alias]['password']
                        );
                }
                return true;
        }
}
我的UsersController代码是

class UsersController extends AppController{
        public $helpers = array('Html','Form');

        public function beforeFilter(){
                parent::beforeFilter();
                $this->Auth->allow('add');
        }
        public function add(){
                if($this->request->is('post')){
                        $this->User->create();
                        if($this->User->save($this->request->data)){
                                $this->Session->setFlash(__('The user has been saved'));
                                return $this->redirect(array('action' => 'index'));
                        }
                        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                }
        }
        public function login(){
                $this->layout = 'mlayout';
                if($this->request->is('post')){
                        debug($this->Auth->login());
                        var_dump($this->request->data);
                        if($this->Auth->login()){
                                $this->Session->setFlash('Logged In');
                                //return $this->redirect($this->Auth->redirectUrl());
                        }else{
                                $this->Session->setFlash(__('Usuario o Contraseña invalido, intentelo de nuevo.'));
                        }
                }
        }
        public function logout(){
                return $this->redirect($this->Auth->logout());
        }
}
AppController代码:

class AppController extends Controller{

        public $components = array('Session',
                'Auth' => array(
                        'loginRedirect' => array(
                                'controller' => 'homes',
                                'action' => 'index'
                        ),
                        'logoutRedirect' => array(
                                'controller' => 'homes',
                                'action' => 'index'
                        ),
                        'authorize' => 'controller'
                )
        );

        public function beforeFilter(){
                $this->Auth->allow('index','view','login');
        }
}
Login.ctp

<table id="formtable">
    <form id="UserForm" method="post" action="/mercadito/users/login">
        <tr>
        <td align="right">Login -> </td>
        <td><input type="text" name="username" style="width: 150px; height: 30px;"/></td>
        </tr>
        <tr>
        <td align="right">Contraseña -> </td>
        <td><input type="password" name="password" style="width: 150px; height: 30px;"/></td>
        </tr>
        <tr>
        <td></td>
        <td align="center"><input type="submit" value="ENTRAR" style="width: 100px; height: 30px;"/></td>
        </tr>
    </form>
</table>

每次都是这样。我更改了
allow->('login')
的位置,然后我检查了我的密码是否经过了哈希处理,结果是正确的。但是问题仍然存在。

尝试扩展组件中的组件变量

AppController:



您应该使用$this->Form->input()方法创建字段,因为Cake期望的字段应该是:

data[User][username]
data[User][password]
<?php echo $this->Form->create('User'); ?>
    <fieldset>
    <?php
        echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    <?php echo $this->Form->submit(__('Login'));?>
    </fieldset>
<?php echo $this->Form->end(); ?>
因此,您的代码应该如下所示:

data[User][username]
data[User][password]
<?php echo $this->Form->create('User'); ?>
    <fieldset>
    <?php
        echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    <?php echo $this->Form->submit(__('Login'));?>
    </fieldset>
<?php echo $this->Form->end(); ?>


我敢打赌,如果您尝试使用空密码,它应该会允许您进入,因为它总是对空变量进行加密:)

确保在数据库中输入其
varchar(40)
varchar(40+)
因为cakephp的哈希算法将生成36个字符的字符串

我不明白你为什么接受了错误的答案。由于您试图使用电子邮件地址进行身份验证,因此应更改login.ctp中的name属性,如下所示:

<input type="text" name="username" style="width: 150px; height: 30px;"/>


我也遇到了同样的问题,我的解决方法是将$this->alias更改为User,所以beforeSave()现在看起来像

public function beforeSave($options = array()) {
if (isset($this->data['User']['password'])) {
    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true; }

请注意,如果您已经登录,它也会返回false!是的,我以前也遇到过这种情况,请尝试注销,然后再次登录。谢谢大家的回复!你说的每句话我都试过了,但始终都是“错的”/
public function beforeSave($options = array()) {
if (isset($this->data['User']['password'])) {
    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true; }