Check mail with PHP

3

Good morning,

Through a registration form, I receive mail among other things. To check that it is an email I do a series of simple checks, however when trying to find the position of certain characters like @ it does not return anything to me.

    $mail = trim($_POST['mail']);
    $pos = strpos($mail, "@");
    $pos2 = strpos($mail, ".com");
    $pos3 = strpos($mail, ".es");

    if ($pos == "false" OR $pos2 == "false" OR $pos3 == "false") {
        $errores .= "Correo electrónico no válido </ br>";
    } else {
        if ($mail != $remail) {
            $errores .= "Las contraseñas no coinciden </ br>";
        }
    }

The mail receives it well but in the variables of $pos I do not receive anything, neither a number nor false. I know that I can put in the type of input email, so that it checks if it is an email, but I prefer to do it manually.

I would appreciate any help.

    
asked by JetLagFox 20.03.2017 в 01:22
source

4 answers

4

Accessing superglobals ($ _ POST, $ _GET, $ _SESSION, $ _SERVER) directly, is not recommended in PHP, instead it is recommended to use cleanup / cleanup functions of the data if what you need is to obtain a value that arrives by POST or GET.

First

In PHP there is a function called filter_input () this function is designed specifically for perform the "cleaning" of the variables, but, only for those that arrive by POST or GET , in a few words, this is the most recommended function to obtain a value that comes from the browser by one of these two methods.

How would it be used?

The first thing you should do is to filter the variable you want to obtain, in this case $_POST['mail'] and instead, use the function that has already been explained.

Staying like this:

$mail = filter_input(INPUT_POST, "mail");

But! , here the explanation does not finish yet. Then we can use the different "flags / flags" that this function allows, and one of these is FILTER_VALIDATE_EMAIL , which according to the PHP documentation:

  

Validate an email address.

This function can return you three values.

  • NULL: If the variable mail does not exist.
  • FALSE: If the email is NOT valid.
  • ! FALSE: If the mail is valid, it returns the contents of the variable.

Conclusion:

You could leave your code like this:

<?php

$mail = filter_input(INPUT_POST, "mail");

if($mail){
    echo "Correo Electronico valido. <br>";
}
else {
    echo "Correo Electronico NO valido. <br>";
}

?>
    
answered by 20.03.2017 / 05:06
source
2

PHP has a function to validate email, it is called filter_var . You just have to send your entire chain to the function, which evaluates if the email is well written.

Example:

DEMO

Here I do it from a function, but you can do it directly if you want:

$mail = trim($_POST['mail']);

// probando la función
verificarEmail("[email protected]");
verificarEmail("[email protected]");


function verificarEmail ($mail)
{
    if (filter_var($mail, FILTER_VALIDATE_EMAIL)) 
    {
        // bien escrito
        echo $mail. " está bien escrito\n";
    }
    else
    {
        // mal escrito
        echo $mail. " está mal escrito\n";
    }
}

Test result

 [email protected] está bien escrito 

 [email protected] está mal escrito
    
answered by 20.03.2017 в 03:44
1

you can check it with the filter_var () function, I hope it helps you.

$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format"; 
}

I leave the source link: link

Greetings.

    
answered by 20.03.2017 в 06:39
0

First, keep in mind that to verify a false value you must use it without quotes.

Now, to do what you want to do, there are 2 options.

The first is that by using strpos () what this gives you back is a position in which is the chain that you are looking for, therefore you could check if the values that you are considering are found by using an if like the following:

LITTLE RECOMMENDED

<?php
 $mail = trim($_POST['mail']);
 $pos = strpos($mail, "@");
 $pos2 = strpos($mail, ".com");
 $pos3 = strpos($mail, ".es");

 if ($pos > -1 AND ($pos2 > -1 OR $pos3 > -1)) {
    echo "El correo electronico es valido.";
 } else {
    echo "El correo electornico no es invalido";
 }
?>

RECOMMENDED

The second one and the one recommended according to the PHP documentation in strpos () is that the checking by using === to verify that it is really a false Boolean. Also keep in mind that you have to group the verification of the domains since it is unlikely that an email containing .com and .es.

The recommended code would be:

<?php
   $mail = trim($_POST['mail']);
   $pos = strpos($mail, "@");
   $pos2 = strpos($mail, ".com");
   $pos3 = strpos($mail, ".es");

   if ($pos === false OR ($pos2 === false AND $pos3 === false)) {
    echo "Correo electrónico no es válido </ br>";
   } else {
    echo "El correo electornico es valido";
   }
?>
    
answered by 20.03.2017 в 02:31