Faced a problem:

On the website in the registration form there is a field in which you need to enter an email (for example: exemple@gmail.com) or a user name (for example: user_name1).

Question: how in the php file of the processing provider to find out what exactly came in the variable $_POST['login'] - the user's email or his username?

  • Why check it for something? see if there is just in the database or not - Alexey Shimansky
  • one
    If dogs are not allowed in the usernames, then you received an email if you have a doggie;) - Visman

2 answers 2

The simplest option is to actually make a query that will search by both values. I do not know which query-builder you are using (and whether it is used).

How clean sql is, it looks like this:

 SELECT * FROM users WHERE (username = '{login}' OR email='{login}') AND 'password' = '{password_hash}' 

In most regular cases, this option will be more than enough.

The most difficult in this query when using a specific query-builder is to find how to group conditions into brackets

    Check regular expression. For example:

     if (preg_match("/^[a-z0-9\.]+@[a-z0-9]+\.[a-z0-9]+$/i", $_POST['login']) == 1) { //email } else { //username } 

    If you want to check E-Mail addresses of potentially containing characters from the alphabet other than English, you can use the method described above by Visman .

     if (strpos($_POST['email'], "@") != false) { //email } else { //username } 

    But this method does not guarantee that they sent you an E-Mail address.

    • And domains for a long time can already be in languages ​​other than English;) - Visman
    • @Visman, you are mistaken, because .rf is: English characters. xn--e1afmkfd.xn--p1ai , which stands for: пример.рф - And
    • @And, I was not mistaken. No normal person will manually type such a bilibed from the keyboard :) - Visman
    • @Visman, well, these are only people who just drive in a field or address bar. I speak in general terms when it happens on the client or server side, where Russian domains will not be supported and you will need to use punycode - a panic code. - And