Auth登录在Cakephp中不起作用

用户名

我编写了一个用户控制器,如果提交的用户名和密码(用cakephp的Security :: hash()=>加密,例如6b0deec0d563224524da45691584643bc78c96ea,没有其他哈希设置)匹配数据库中的行,则应该登录用户但这是行不通的,我也不知道为什么。

这是我的UsersController.php的片段

public function add() {
    $this->set("title_for_layout", "Register");

    if($this->request->is("post")) {
        $this->User->set($this->request->data);

    if($this->User->save(array("password" => Security::hash($this->request->data["User"]["password"])))) {
            $this->Session->setFlash(__("Successfully registred."), "flash_success");
            $this->redirect("/login");
    } else {
        $this->Session->setFlash(__("Validation failed."), "flash_danger");
    }
    }
}

注册工作正常,并且在数据库中创建了一行,其中有一个列“ username”,其中包含普通用户名,例如“ myuser”和“ password”,其中包含哈希字符串。我认为问题不能在这里解决。

这是我的UsersController.php的另一个代码段

public function login() {
    $this->set("title_for_layout", "Login");

    if($this->request->is("post")) {
        if($this->Auth->login()) {
            $this->Session->setFlash("Login successfull.", "flash_success");
        } else {
            $this->Session->setFlash("Login failed.", "flash_danger");
        }
    }
}

这是视图login.ctp

<?php echo $this->Form->create('User'); ?>
<?= $this->Form->input("username"); ?>
<?= $this->Form->password("password"); ?>
<?= $this->Form->end("submit"); ?>

这是我的问题:登录始终失败。另外,我在$component阵列中没有任何设置

我该如何解决这个问题?

维卡什·帕塔克(Vikash Pathak)

如果您使用cakePHP 2.x,则可以在模型的回调函数中将密码加密设置为beforeSave()为

<?php
// app/Model/User.php
App::uses('AuthComponent', 'Controller/Component');
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;
}

?>

有关更多信息,请单击链接如果您仍想在控制器中加密密码,则可以使用类似的代码。

public function add() {
    $this->set("title_for_layout", "Register");

    if($this->request->is("post")) {
        $this->User->set($this->request->data);

    if($this->User->save(array("password" => $this->Auth->password($this->request->data["User"]["password"])))) {
            $this->Session->setFlash(__("Successfully registred."), "flash_success");
            $this->redirect("/login");
    } else {
        $this->Session->setFlash(__("Validation failed."), "flash_danger");
    }
    }
}

?>

如果您使用的是CakePHP 2.4或更高版本,请遵循此处的文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

默认登录在cakephp 3.2中不起作用

Firebase登录在Flutter Android应用中不起作用

指纹登录在Win10中不起作用

登录在 Spring Security 的自定义登录页面中不起作用

登录在Django中的自定义用户模式中不起作用

Auth注销在CakePHP 2.x中不起作用

使用cookie登录在ASP.net中不起作用

Android Firebase身份验证:联合Google登录在Oreo中不起作用

facebook应用程序登录在facebook画布中不起作用

流星:Google登录在iPhone / iPad上不起作用

登录在我的项目中不起作用

匿名FTP登录在Windows 10上不起作用

Laravel登录(Auth :: attempt)不起作用

make:auth()登录/注册不起作用

我无法在 admin 中登录,并且在自定义用户之后登录在 Django 项目中不起作用

DESC中的订单记录在SequelizeJS中不起作用

使用会话的Codeigniter登录在iPad和其他移动浏览器中不起作用

使用facebook按钮登录在结帐登录页面上不起作用,但在登录页面上起作用

更改tmp目录在Spark中不起作用

屏幕记录在模拟器中不起作用

Cakephp 2.x Auth 在 Chrome 74.0 版本中不起作用,但在 mozilla 66.0 版本中起作用

CakePHP-基本登录功能不起作用

cakephp4身份验证登录不起作用

CakePHP身份验证(登录/注销)不起作用

图像源在cakePHP中不起作用

AutoRender = False在CakePHP中不起作用

流星登录在部署到* .meteor.com时不起作用

Unity Facebook登录在Unity的发布模式下不起作用

Rails / Devise登录在Safari上不起作用(422 / CSRF错误)