cakephp电话号码验证

cakephp电话号码验证

问题描述:

我是cakephp 2.x的新手,所以我不知道如何做到这一点..我想从他的电子邮件地址和电话号码登录用户..我的意图是,如果数据库中的数字是这个“12345”和用户正试图通过这个号码“+12345”登录,他可以登录到系统..我已经写了一个代码,但我不知道如何使用此或调整我的代码在auth组件作为AUTH组件autometically登录用户..cakephp电话号码验证

这里是我的控制器

public function beforeFilter() { 
    parent::beforeFilter(); 


    $this->Auth->authenticate = array(
     'Authenticate.Cookie' => array(
      'fields' => array(
       'username' => 'email', 
       'password' => 'password' 
      ), 
      'userModel' => 'User', 
      'scope' => array('User.active' => 1) 
     ), 
     'Authenticate.MultiColumn' => array(
      'fields' => array(
       'username' => 'email', 
       'password' => 'password' 
      ), 
      'columns' => array('email', 'mobileNo'), 
      'userModel' => 'User', 
     ) 
    ); 
} 




public function login() { 


    if ($this->Auth->login() || $this->Auth->loggedIn()) { 
     $this->redirect('/users/dashboard'); 
    }else{ 
    $this->layout='logindefault'; 
    $this->set('title_for_layout', 'Account Login'); 
    /*$this->Auth->logout(); 
    $cookie = $this->Cookie->read('Auth.User'); */ 


    if ($this->request->is('post')) { 




     if ($this->Auth->login() || $this->Auth->loggedIn()) { 
      if ($this->Session->check('Auth.User')){ 


      $this->_setCookie($this->Auth->user('idUser')); 
      $this->redirect('/users/dashboard'); 

     } }else { 
      $this->Session->setFlash('Incorrect Email/Password Combination'); 
     } 
    }} 
} 

这里是我试图添加代码..

$mobileNo='+123456789'; 
    if (strpos($mobileNo,'+') !== false) { 
     $mobileNo=str_replace("+", "",$mobileNo); 
    } 

?>

该文档(http://book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules)对自定义的验证规则的示例。

但您的用例与验证无关(对于登录,没有正常验证,只能应用“范围”)。 它关于自定义进入登录的数据。 您可以使用login()方法在Auth-> login()调用之前修改传入数据,也可以使用新的2.x方法执行此操作:使用自定义身份验证适配器。然后

// in your AppController::beforeFilter() method 
$this->Auth->authorize = array('PhoneNumber'); 

和你******中国适配器应该叫PhoneNumberAuthenticate并放在/控制器/组件/验证。

有关其他适配器如何工作并将其应用于您自己的详细信息,请参阅https://github.com/ceeram/Authenticate

但因为你还使用“电子邮件”登陆,你可能是最好的使用替代选择在您的登录方法:

public function login() { 
    $this->request->data['User']['email'] = $this->_replace($this->request->data['User']['email']); 
    if ($this->Auth->login()) {...} 

与_replace()包含您replacment代码。

+0

我不知道我能做到这一点......如果您已实现或此功能的工作,如果你不介意,否则它的工作要做。作为我被困在此之前共享代码这里现在 – hellosheikh

+0

没有得到你..谁我替​​换...为什么我应该替换...我想要做的是..我想从电子邮件,数字和+号码登录用户.. – hellosheikh

+0

确切 - arent你在你的方法中使用'str_replace'来“统一”格式?只需将该代码放在那里(作为控制器或模型中的自己的方法 - 或内联)。 – mark

我已经做到了这一点,它的工作原理

我只是$这个 - > Auth->登录() $这个 - >请求 - >数据[“用户”] [“电子邮件”之前加上这一行] = $ mobileNo;

来源:string concatenation when user log in Cakephp