email-validatie-snippet

Gesponsorde koppelingen

PHP script bestanden

  1. email-validatie-snippet

« Lees de omschrijving en reacties

Versie zonder preg_match (werkt met filter_var functie)

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
function checkmail($email){

    /* Check OS script is executed on*/
    /* Checkdnsrr Windows support from php 5.3*/

    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' and version_compare(PHP_VERSION, '5.3.0') === -1) {
        /* PHP version doesn't support getmxrr and checkdnsrr*/
        /* Syntax control function here*/

        return filter_var($email,FILTER_VALIDATE_EMAIL) != false;
    }


     /* PHP version higher or equal to 5.3.0*/
     /* Function by Hipska @ http://www.phphulp.nl/php/scripts/11/1622/ */

     if(preg_match('#.+@(?<host>.+)#',$email,$match) > 0 and getmxrr($match['host'],$mxhosts)){
          /* Found MX records*/
          
          $valid = false;

          /* Walkt through MX records for valid*/
          while($host = next($mxhosts) and !$valid){
              /* IPv4 or IPv6 will do*/
              $valid = checkdnsrr($host, 'A') or checkdnsrr($host,'AAAA');
          }

                
           return $valid;
     }

            
     /* No valid email --> no correct host name or no MX records found*/
     return false;
}

?>


Versie met preg_match:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
function checkmail($email){

    /* Check OS script is executed on*/
    /* Checkdnsrr Windows support from php 5.3*/

    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' and version_compare(PHP_VERSION, '5.3.0') === -1) {
        /* PHP version doesn't support getmxrr and checkdnsrr*/
        /* Syntax control function here*/

        return preg_match('/^\w[-.\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/', $email);
    }


     /* PHP version higher or equal to 5.3.0*/
     /* Function by Hipska @ http://www.phphulp.nl/php/scripts/11/1622/ */

     if(preg_match('#.+@(?<host>.+)#',$email,$match) > 0 and getmxrr($match['host'],$mxhosts)){
          /* Found MX records*/
          
          $valid = false;

          /* Walkt through MX records for valid*/
          while($host = next($mxhosts) and !$valid){
              /* IPv4 or IPv6 will do*/
              $valid = checkdnsrr($host, 'A') or checkdnsrr($host,'AAAA');
          }

                
           return $valid;
     }

            
     /* No valid email --> no correct host name or no MX records found*/
     return false;
}

?>


Zelf testen
Om het zelf even te testen (zie update bij beschrijving):

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

include "functies.php";

if($_SERVER['REQUEST_METHOD'] == "POST") {

    echo 'Result custom checkmail:<br />';
    echo (checkmail($_POST['email'])) ? 'valid' : 'invalid';
    
    echo '<br /><br />';
    
    echo 'Result filter_var:<br />';
    echo (is_string(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))) ? 'valid' : 'invalid';
    
    echo '<br /><br />';
    
    echo 'Result forced preg_match:<br />';
    echo (preg_match('/^\w[-.\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/', $_POST['email'])) ? 'valid' : 'invalid';
    echo '<br /><br /><a href="javascript: history.go(-1)"class="link_orange">Klik hier</a> om terug te gaan en opnieuw te proberen';
}

else {
    echo 'Test email validation:<br /><br />
    <form method="post" action="'
.$_SERVER['PHP_SELF'].'">
    Email: <input type="text" name="email">
    <input type="submit" value="verzenden" name="verzenden">
    </form>'
;
}

?>


Snel in elkaar geflanst..... (zorg dat je in de include de functie op een manier in je script krijgt)

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.