在 CakePHP 的 Auth 组件中使用用户名而不是电子邮件 [英] Using username instead of email in CakePHP's Auth Component
本文介绍了在 CakePHP 的 Auth 组件中使用用户名而不是电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 CakePHP 的 Auth 组件,我如何允许用户使用他们的用户名"或电子邮件"字段作为用户名,并使用通行证"字段作为他们的密码来进行身份验证?
解决方案
同时使用(用户名和电子邮件)作为用户名"是什么意思?
好的,所以您希望 Auth 在数据库中查看用户名和电子邮件字段以与用户输入的用户名"进行比较?然后这样做:
<块引用>function beforeFilter() {父::beforeFilter();$this->Auth->fields = array('username' => 'username', 'password' => 'pass');$this->Auth->autoRedirect = false;}函数登录(){如果 ($this->Auth->user()) {$this->redirect($this->Auth->redirect());} else if (!empty($this->data)) {$this->Auth->fields = array('username' => 'email', 'password' => 'pass');$this->data['User']['email'] = $this->data['User']['username'];if($this->Auth->login($this->data))$this->redirect($this->Auth->redirect());}}
Using CakePHP's Auth Component, how do I allow users to authenticate by using either their "username" or "email" field as a username, and a "pass" field as their password?
解决方案
what does "using (username and email) both as username " mean?
Edit: ok, so you want Auth to look in both username and email fields in the db to compare to the "username" that the user enters? then do this:
function beforeFilter() { parent::beforeFilter(); $this->Auth->fields = array('username' => 'username', 'password' => 'pass'); $this->Auth->autoRedirect = false; } function login(){ if ($this->Auth->user()) { $this->redirect($this->Auth->redirect()); } else if (!empty($this->data)) { $this->Auth->fields = array('username' => 'email', 'password' => 'pass'); $this->data['User']['email'] = $this->data['User']['username']; if($this->Auth->login($this->data))$this->redirect($this->Auth->redirect()); } }
这篇关于在 CakePHP 的 Auth 组件中使用用户名而不是电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文