【问题标题】:cakephp phone number validationcakephp电话号码验证
【发布时间】:2013-06-27 08:31:22
【问题描述】:

我是 cakephp 2.x 的新手,所以我不知道该怎么做。我想从用户的电子邮件地址和电话号码登录用户。如果数据库中的号码是“12345”,我的意图是什么并且用户正在尝试通过此号码“+12345”登录他可以登录系统..我已经编写了一个代码,但我不知道如何使用它或将身份验证组件中的代码调整为身份验证组件正在自动记录用户..

这是我的控制器

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

?>

【问题讨论】:

    标签: cakephp cakephp-2.0 cakephp-2.1


    【解决方案1】:

    文档 (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');
    

    然后您的 PhoneNumber 适配器应称为 PhoneNumberAuthenticate 并放置在 /Controller/Component/Auth 中。

    请参阅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() 包含您的替换代码。

    【讨论】:

    • 我不知道我该怎么做...如果您之前已经实现或正在使用此功能,那么如果您不介意的话,请分享代码...因为我被困在这里了现在
    • 没有得到你..我要替换谁...我为什么要替换...我想做的是..我想从电子邮件、号码和+号码登录用户..
    • 完全正确-您不是在您的方法中使用str_replace 来“统一”格式吗?只需将该代码放在那里(作为控制器或模型中自己的方法 - 或内联)。
    【解决方案2】:

    我已经这样做了,它可以工作

    我在 $this->Auth->login() 之前添加了这一行 $this->request->data['User']['email'] = $mobileNo;

    来源:string concatenation when user log in Cakephp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 2011-09-05
      相关资源
      最近更新 更多