login with Cpf or email in php

Asked

Viewed 792 times

0

I have a little problem when making a login system which user should be able to authenticate by your email or Cpf.

I am making the comparison with the value of the login field received, if it is number I do research with Cpf and if it is not I do with email. See below:

 if (is_numeric($_POST['email_cpf'])) {
     //pesquisa com cpf
 }else{
     //pesquisa com e-mail
 }

This works, the problem is that when the user puts in the login field Cpf in the format with separators 000.000.000-00, this is given as a string and therefore does searches with the email.

I made a function to remove the tabs, but this is bad because if the email for example has dashes or dots, they will also be removed

How do I fix it?

  • You can define a regular expression that checks if the value matches a CPF.

2 answers

1


Before you check if the user has only typed numbers, remove the dots and dashes using the function str_replace as follows:

$login = $_POST['email_cpf'];
$login = str_replace('.', '', $login);
$login = str_replace('-', '', $login);

At this point you will already have what the user typed without dots or dashes in the variable $login. If the variable contains only numbers you check by CPF, otherwise, you take what the user initially typed and check the email:

if (is_numeric($login)) {
    //pesquisa com cpf
}else{
    $login = $_POST['email_cpf'];
    //pesquisa com e-mail
}

You can also do this in a more simplified way:

if (is_numeric(str_replace(array('.', '-'), $_POST['email_cpf']))) {
    //pesquisa com cpf
}else{
    //pesquisa com e-mail
}
  • I already tried this, but the problem is that if the user logs in with email instead of Cpf, and the email has dashes or dots, they will also be removed and therefore would not work.

  • 1

    I edited my answer, take a look...

  • 1

    The removal should be valid only for checking next to the is_numeric, but the original value shall remain unchanged.

  • But what the user typed is the same way in the variable $_POST['email_cpf']. We only change the variable $login. Anyway, I added a simplified way to do it all. .

  • 1

    @Roberto, the role str_replace accepts a array as a parameter, then you can do everything in just one call.

  • That I didn’t know, anyway I made the change. Vlw @Andersoncarloswoss

Show 1 more comment

1

With regular expression, you can do something like:

if (preg_match("/^\d{3}\.?\d{3}\.?\d{3}-?\d{2}$/", $valor)) {
    // É CPF
} else {
    // É e-mail
}

Thus, the separator characters will be optional, being able to either inform the CPF with only numbers or with the separators, since it has the 11 digits.

With the characters ^ and $ the beginning and end of the expression is defined, ensuring that there is nothing more than the CPF in the value. Already the \d{x} creates groups of x digits.

See working on Ideone.

  • Thank you, but I thought better the solution of Roberto, because the user can sometimes type one more point or put the dash in the wrong place and etc.. And with the preg_match any of these small errors will fall into validation with email, and the way Oberto did not. And there’s an error in that regex of yours, but still thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.