2013-06-27 53 views
0

我在CakePHP 2.x.中创建了一个登录系统,用户可以从他的电子邮件地址和他的电话号码登录。我想要的是,如果用户提供电话号码,例如“123456789”或“+123456789”,他可以登录到系统。有时候现在只有一个任务可以通过这个来实现。如果数据库中的数字是“123456789”,他不能从这个“+123456789”登录。验证CakePHP中的用户电话号码

我想我已经在模型类应用规则...

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

它可以检查电话号码的条件,如检查+字符串,并将其删除,并检查剩余的数字。 –

+0

@SiddeshBhalke怎么样? – hellosheikh

+0

@ HelloSheik在这里粘贴你的登录行动代码,让我看看你在做什么..! –

回答

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

我在哪里把你的功能放到我的代码中,因为我使用auth组件检查和自动记录日志..你能否把你的函数放到我的代码中..如果你不介意 – hellosheikh

+0

我已经修改的东西,只要给出适当的模型和字段名称,如果需要的话。让我知道结果.. !! –

+0

以及我在这里提到的是,我有一个输入框名称“电子邮件”,其中我收到一个手机号码和电子邮件?那么你的语法是否正确? – hellosheikh