验证登录用电子邮件或手机CakePHP的 [英] Auth login with email or mobile Cakephp
问题描述
我的工作CakePHP的2.x.i在我的数据库名称用户有一个表,它有4个领域的的id,电子邮件,密码和mobileNo
我有两个领域在我的 login.ctp
< PHP
回声$这个 - >&形式 - GT;创建();回声$这个 - >&形式 - GT;输入(电子邮件);
回声$这个 - >&形式 - GT;输入('密码');
回声$这个 - >&形式 - GT;结束('提交');
?>
我想什么是我想要登录他的mobileNo用户太(如果他输入手机号码,而不是电子邮件地址),就像Facebook已经做..he可以与喜的电子邮件地址登录或mobileno。我不想创建另一个输入字段..我不知道我能做到这一点这里是我的code
的AppController
类AppController的扩展控制器{
公共$组件=阵列(
会话,
'验证'=>阵列(
loginRedirect'=>阵列(控制器= GT;'用户','行动'=>管理),
logoutRedirect'=>阵列(控制器= GT;'用户','行动'=>管理),
authError'=>中你不能访问该页面,
'授权'=>阵列(控制器),
'身份验证'=>阵列(
形= GT;阵列(
'域'=>阵列('用户名'= GT;电子邮件)
)))
);
)
);
公共职能isAuthorized($用户){
} 公共职能beforeFilter(){
$这个 - > Auth->允许('指数');
}
}
UserController中
公共职能登录()
{
如果($这个 - >请求 - '是('后')){
如果(这 - $> Auth->登录()){
$这个 - >重定向($这个 - > Auth->重定向());
}其他{
$这个 - >会话级> setFlash('您的email /密码组合是不正确');
}
}
}
使用此code在应用程序控制器beforefilter();
AuthComponent :: $ sessionKey ='Auth.User';
如果($这个 - >请求 - '是('后')及和放大器; $这个 - >动作=='登录'){
$ USERNAME = $这个 - >请求 - >数据['用户'] [电子邮件];
如果(filter_var($用户名,FILTER_VALIDATE_EMAIL)){
$这个 - > Auth->身份验证=阵列(
形= GT;阵列(
'域'=>阵列(
用户名= GT; 电子邮件,//默认为用户名中的usermodel
'密码'=> '密码'),//默认为密码中的usermodel
//'范围'=>阵列('User.status'=>'1'),
'的usermodel'=> '用户'
)
);
}其他{
$这个 - > Auth->验证[形式] [字段] [电子邮件] ='移动';
$这个 - >请求 - >数据['用户'] ['移动'] = $的用户名;
未设置($这个 - >请求 - >数据['用户'] ['邮件']);
$这个 - > Auth->身份验证=阵列(
形= GT;阵列(
'域'=>阵列(
用户名= GT; 移动,//默认为用户名中的usermodel
'密码'=> '密码'),//默认为密码中的usermodel
//'范围'=>阵列('User.status'=>'1'),
'的usermodel'=> '用户'
)
); }
}
$这个 - >请求 - >数据['用户'] [电子邮件]是我的表单字段中,我发送电子邮件或手机。
I am working on cakephp 2.x.i have a table in my database name user and it has 4 fields id, email, password and mobileNo
i have two fields in my login.ctp
<?php
echo $this->form->create();
echo $this->form->input('email');
echo $this->form->input('password');
echo $this->form->end('submit');
?>
what i want is i want to login the user from his mobileNo too(if he typed mobile number rather then email address) just like facebook has done ..he can either login with hi email address or mobileno .i dont want to create another input field.. i dont know how can i do this here is my code
AppController
class AppController extends Controller {
public $components = array(
'Session',
'Auth'=>array(
'loginRedirect'=>array('controller'=>'users', 'action'=>'admin'),
'logoutRedirect'=>array('controller'=>'users', 'action'=>'admin'),
'authError'=>"You can't access that page",
'authorize'=>array('Controller'),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)))
);
)
);
public function isAuthorized($user) {
}
public function beforeFilter() {
$this->Auth->allow('index');
}
}
UserController
public function login()
{
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Your email/password combination was incorrect');
}
}
}
Use this code in app controller in beforefilter();
AuthComponent::$sessionKey = 'Auth.User';
if ($this->request->is('post') && $this->action == 'login') {
$username = $this->request->data['User']['email'];
if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array(
'username' => 'email', //Default is 'username' in the userModel
'password' => 'password'), //Default is 'password' in the userModel
// 'scope'=>array('User.status' => '1'),
'userModel' => 'User'
)
);
}else{
$this->Auth->authenticate['Form']['fields']['email'] = 'mobile';
$this->request->data['User']['mobile'] = $username;
unset($this->request->data['User']['email']);
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array(
'username' => 'mobile', //Default is 'username' in the userModel
'password' => 'password'), //Default is 'password' in the userModel
// 'scope'=>array('User.status' => '1'),
'userModel' => 'User'
)
);
}
}
$this->request->data['User']['email'] is my form field in which I am sending email or mobile.
这篇关于验证登录用电子邮件或手机CakePHP的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!