【问题标题】:Validate user phone number in CakePHP在 CakePHP 中验证用户电话号码
【发布时间】:2013-06-27 05:50:26
【问题描述】:

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

【问题讨论】:

  • 可以在检查电话号码条件的同时完成,比如检查+字符串并删除它,&检查剩余号码..!!
  • @SiddeshBhalke 怎么样?
  • @HelloSheik 在此处粘贴您的登录操作代码,让我看看您做得如何..!
  • @SiddeshBhalke 我更新了我的问题

标签: regex cakephp cakephp-2.0 cakephp-2.1


【解决方案1】:

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

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

【讨论】:

  • 我在哪里把你的函数放到我的代码中,因为我正在使用自动检查和记录的 auth 组件.. 你能把你的函数放到我的代码中吗.. 如果你不介意
  • 检查上面我修改过的东西,如果需要,只需给出正确的模型和字段名称..!!让我知道结果..!!
  • 好吧,我在这里确实提到的是,我有一个输入框名称“电子邮件”,其中我得到了一个手机号码和电子邮件?那么您的语法是否正确?
  • 试着看看你在数据请求中得到了什么值..!! pr($this->request->data());
  • 浏览器上没有打印出来..如果你想知道的话,我有一个电子邮件输入框..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多