验证CakePHP中的用户电话号码

时间:2013-06-27 05:50:27

标签: regex cakephp cakephp-2.0 cakephp-2.1

我在CakePHP 2.x.中创建了一个登录系统,用户可以从他的电子邮件地址和电话号码登录。我想要的是如果用户提供电话号码,例如“123456789”或“+123456789”,他可以登录系统。有时现在只能完成一项任务。如果数据库中的数字是“123456789”,则他无法从此“+123456789”登录。

我想我必须在模型类中应用规则......

$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');
    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');
      }
    }
  }
}

1 个答案:

答案 0 :(得分:4)

试试这个,在检查电话号码之前,请点击此行,

  public function login() {


 $this->request->data['User']['mobile']=$this->request->data['User']['mobile'];
   if (strpos($this->request->data['User']['mobile'],'+') !== false) {
         $this->request->data['User']['mobile']=str_replace("+", "",$this->request->data['User']['mobile']);
   }
      $this->requst->data['User']['mobile']=$this->request->data['User']['mobile'];

  if ($this->Auth->login($this->request->data)) {
    $this->redirect('/users/dashboard');
  }else{
    $this->layout='logindefault';
    $this->set('title_for_layout', 'Account Login');
    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');
      }
    }
  }
}