身份验证登录不';我不能在cakephp中工作
我已经编写了一个用户控制器,如果提交的用户名和密码(使用cakephp的Security::hash()加密)与数据库中的一行匹配,则该控制器应该登录用户。但它不起作用,我也不知道为什么 这是我的userscoontroller.php的一个片段身份验证登录不';我不能在cakephp中工作,php,cakephp,authentication,Php,Cakephp,Authentication,我已经编写了一个用户控制器,如果提交的用户名和密码(使用cakephp的Security::hash()加密)与数据库中的一行匹配,则该控制器应该登录用户。但它不起作用,我也不知道为什么 这是我的userscoontroller.php的一个片段 public function add() { $this->set("title_for_layout", "Register"); if($this->request->is("post")) {
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");
}
}
}
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");
}
}
}
<?php echo $this->Form->create('User'); ?>
<?= $this->Form->input("username"); ?>
<?= $this->Form->password("password"); ?>
<?= $this->Form->end("submit"); ?>
注册工作正常,在数据库中创建了一行,其中我有列“username”,其中包含普通用户名,例如“myuser”和“password”,其中包含散列字符串。我认为这个问题在这里解决不了
这是myUsersController.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");
}
}
}
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");
}
}
}
<?php echo $this->Form->create('User'); ?>
<?= $this->Form->input("username"); ?>
<?= $this->Form->password("password"); ?>
<?= $this->Form->end("submit"); ?>
下面是login.ctp的视图
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");
}
}
}
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");
}
}
}
<?php echo $this->Form->create('User'); ?>
<?= $this->Form->input("username"); ?>
<?= $this->Form->password("password"); ?>
<?= $this->Form->end("submit"); ?>
我的问题是:登录总是失败。此外,在$component
数组中没有任何设置
如何解决这个问题?如果您使用的是cakePHP 2.x,那么您可以在模型的回调函数beforeSave()中将密码加密设置为
如果您使用的是cakePHP 2.4或更高版本,请按照文档进行操作。不应在控制器中手动哈希。请参阅上的文档。在set()之后通过数据调用save()听起来像是很容易出错的事情。为了完整性:您还缺少create()。现在我改变了方式,将保存一个新用户,但这并不能解决我的问题。数据库中保存的数据与以前相同(但这种新方法更干净,谢谢)。现在我发现,sql查询的
$this->Auth->login()
只在db中查找用户名,而不是密码。查询如下所示:[…]'users'作为'User',其中'User'。'username'='xxx'限制1
。但是查询中的密码在哪里?请尝试$this->Form->input(“password”)
,即使两者都可以使用。并发布控制器身份验证配置。这可能就是您的配置关闭的地方。