Question

I have a script that will validate an email address & I think this is the only solution to validate emails on form submission? I end up with this script

<?php
if(isset($_POST['email']))

{
    $email = $_POST['email'];  

    if (strpos($email, '@')) 
    {
        $first = end(explode("@", $email));

        if(checkdnsrr($first, 'MX')) 
        {
            $validate = 'Valid email.';
        }
        else 
        {
            $validate = 'Invalid email.';   
        }
    }
    else
    {
        $validate = 'Invalid email.';
    }

    echo $validate;
}
?>  
<form method="POST">
<input type="text" name="email">
<input type="submit" value="submit">
</form>

It runs correctly but I have this error Strict standards: Only variables should be passed by reference.

Is there any way to remove the ERROR? and IMPROVE the code? Is there any email validation so far that is really validate emails?

Était-ce utile?

La solution 3

The reson is, end(explode("@", $email)); function causes the error, end() needs a reference, for that you can try using the below code,

$EmailArray = explode("@", $email);
$first = end($EmailArray);

mixed end ( array &$array )

end() advances array's internal pointer to the last element, and returns its value.

Parameter:

array: This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

Return Values: Returns the value of the last element or FALSE for empty array.

Autres conseils

you could do something like this

<?php
if(isset($_POST['email']))

{
    $email = $_POST['email'];  

    if (strpos($email, '@')) 
    {
        $first = explode("@", $email);

        $validate = checkdnsrr($first[1], 'MX') ? 'Valid email.' : 'Invalid email.';
    }
    else
    {
        $validate = 'Invalid email.';
    }

    echo $validate;
}
?>  
<form method="POST">
<input type="text" name="email">
<input type="submit" value="submit">
</form>

you can try like below as a example

 <?php

    function domain_exists($email, $record = 'MX'){
        list($user, $domain) = explode('@', $email);
        return checkdnsrr($domain, $record);
    }
    if(isset($_POST['email'])){
        $email = $_POST['email'];
        if(domain_exists($email)) {
            echo('This MX records exists; I will accept this email as valid.');
        }
        else {
            echo('No MX record exists;  Invalid email.');
        }
    }
    ?>

But, this isn't reliable. dns isn't reliable in general. It's best-effort. It can also take a long time when an invalid domain is provided, so you need to consider a potential DOS of your system if someone hammers your script with a lot of invalid/offline domains.

In (little known) fact, domains don't even need to have an MX record for mail to be delivered. Mail delivery is supposed to fall back to using the A record for the host specified. That may not really happen much these days, but in theory..

so you can validate your email with preg match like

 //$email = "abc123@lolhaha"; // Invalid email address 
    $email = "somebody@somesite.com"; // Valid email address 

    $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 

    if (preg_match($regex, $email)) {
         echo $email . " is a valid email. We can accept it.";
    } else { 
         echo $email . " is an invalid email. Please try again.";
    } 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top