2013-06-27 51 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