Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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-登录函数的问题_Php_Mysql_Cakephp - Fatal编程技术网

CakePHP-登录函数的问题

CakePHP-登录函数的问题,php,mysql,cakephp,Php,Mysql,Cakephp,这是密码 userscoontroller.php <?php class UsersController extends AppController { public function login() { if ($this->request->is('post')) { if ($this->Auth->login()) { return $this->redirec

这是密码

userscoontroller.php

<?php

class UsersController extends AppController
{
    public function login()
    {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirect());
            }
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }

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

?>
<?php

class User extends AppModel
{

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

        return true;
    }
}
<?php

class AppController extends Controller
{
    public $components = array(
        'DebugKit.Toolbar',
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'books',
                'action' => 'view'
            ),
            'logoutRedirect' => array(
                'controller' => 'pages',
                'action' => 'home'
            ),
        )
    );
}

login.ctp
(查看文件)

表名:用户

我是CakePHP新手,我刚刚编写了简单登录操作的代码,
当我尝试使用正确的凭据登录时,它总是显示不正确的用户名和密码

$this->Auth->login()。请给我一个解决办法。谢谢。你应该试试这个

在CakePHP数据库中,表名应该是复数。模型名称应为带CamelCase的单数名词。控制器应为复数形式,且必须添加CamelCase add后缀控制器。视图文件夹名称始终与控制器复数名称相同(无后缀),且
。ctp
文件名应为动词

AppController.php

<?php
class AppController extends Controller {

    public $components = array(
        'RequestHandler','Session',
        'Auth' => array(
            'Autoredirect'=>false,
            'loginRedirect' => array('controller' => 'books', 'action' => 'view'),
            'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
            'authError' => 'Did you really think you are allowed to see that?',
        )
    );
}    
?>
<?php

class UsersController extends AppController
{
    var $helpers = array('Html', 'Form','Js','Session');
        public $components = array(
            'RequestHandler',
            'Session'
    );

public function login(){
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                 $this->Session->setFlash('You are successfully login','default',array('class'=>'alert alert-success'));  
                 return $this->redirect($this->Auth->redirect());
            }
          return  $this->Session->setFlash('Invalid username and password,please try again','default',array('class'=>'alert alert-danger'));    
        }
    }
}
?>  
<?php echo $this->Session->flash(); ?>
   <?php echo $this->Form->create('User'); ?>  
      <div class="form-group">
         <label>Email</label>
        <?php echo $this->Form->input('User.username');?>
        </div>
       <div class="form-group">
         <label>Password</label>
        <?php echo $this->Form->input('User.password');?>
        </div>
        <div class="form-group">
     <?php echo $this->Form->button('Login',array('type'=>'submit','class'=>'btn btn-primary btn-block')); ?>   
</div>  
  <?php echo $this->Form->end(); ?>
 App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
    class AppModel extends Model {
        public $components = array(
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => array(
                        'className' => 'Simple',
                        'hashType' => 'sha1'
                    )
                )
            )
        )
    );
        public function beforeSave($options = array()) {
            if (!empty($this->data[$this->alias]['password'])) {
                $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha1'));
                $this->data[$this->alias]['password'] = $passwordHasher->hash(
                    $this->data[$this->alias]['password']
                );
            }
            return true;
        }
    }
    ?>
User.php(用户模型)



我意外地限制了数据库中的密码字段,使密码部分存储在数据库中,这是导致此问题的原因。

您是否正在实施密码哈希器?谢谢您的回答,但它不再有效同一问题:(
 App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
    class AppModel extends Model {
        public $components = array(
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => array(
                        'className' => 'Simple',
                        'hashType' => 'sha1'
                    )
                )
            )
        )
    );
        public function beforeSave($options = array()) {
            if (!empty($this->data[$this->alias]['password'])) {
                $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha1'));
                $this->data[$this->alias]['password'] = $passwordHasher->hash(
                    $this->data[$this->alias]['password']
                );
            }
            return true;
        }
    }
    ?>
<?php
        class User extends AppModel
        {
         public $validate = array(
            'username' => array(
                'alphaNumeric' => array(
                    'rule' => 'alphaNumeric',
                    'required' => true,
                    'message' => 'Letters and numbers only'
                ),

            ),
            'password' => array(
                'rule' => array('minLength', '8'),
                'message' => 'Minimum 8 characters long'
            ),
        );
    }
    ?>