Posted By

mladoux on 09/21/10


Tagged

email php validation security IPv6


Versions (?)

Email Validation (IPv6 Version)


 / Published in: PHP
 

URL: http://mark.haktstudios.com/

This is a version of the previous script that is compliant with IPv6, it requires PHP 5 or newer, so yeah, if you don’t have it, it will fail horribly ( due to the AAAA record check ).

  1. /******************************************************************************
  2. Is email valid??
  3.  
  4. This function verifies that the email address complies with the following
  5. standards:
  6.  
  7. RFC 822
  8. RFC 1035
  9. RFC 2821
  10. RFC 2822
  11.  
  12. It also ensures that the domain actually resolves.
  13. /******************************************************************************/
  14.  
  15. function e_valid($email)
  16. {
  17. // get position of the final @ symbol
  18. $index = strrpos($email, "@");
  19. if (is_bool($index) && !$index) return false;
  20.  
  21. // split up the email address into domain and local parts
  22. $domain = substr($email, $index+1);
  23. $local = substr($email, 0, $index);
  24.  
  25. // determine string length
  26. $l_len = strlen($local);
  27. $d_len = strlen($domain);
  28.  
  29. // verify strings aren't too short or too long
  30. if ($l_len < 1 || $l_len > 64) return false;
  31. if ($d_len < 1 || $d_len > 255) return false;
  32.  
  33. // verify that we don't start or end with a .
  34. if ($local[0] == '.' || $local[$l_len-1] == '.') return false;
  35. if ($domain[0] == '.' || $domain[$d_len-1] == '.') return false;
  36.  
  37. // verify we don't have two .'s in succession
  38. if (preg_match('/\\.\\./', $local)) return false;
  39. if (preg_match('/\\.\\./', $domain)) return false;
  40.  
  41. // check for disallowed characters
  42. if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) return false;
  43. if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
  44. str_replace("\\\\","",$local)))
  45. {
  46. // check for invalid escape sequences in the local part
  47. if ( ! preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local)))
  48. return false;
  49. // check for valid DNS records
  50. if ( ! (checkdnsrr($domain,"MX") || checkdnsrr($domain,"A") || checkndsrr("AAAA")))
  51. return false;
  52. }
  53.  
  54. // return true, the email is valid.
  55. return true;
  56. }

Report this snippet  

You need to login to post a comment.