Home  >  Article  >  Backend Development  >  PHP code to verify the validity and correctness of the email entered by the user

PHP code to verify the validity and correctness of the email entered by the user

WBOY
WBOYOriginal
2016-07-25 09:03:43883browse
  1. function validate_email($email){

  2. $exp="^[a-z'0-9]+([._-][a-z'0 -9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
  3. if(eregi($exp,$email)){ / /First use regular expressions to verify the validity of the email format
  4. if(checkdnsrr(array_pop(explode("@",$email)),"MX")){//Then use checkdnsrr to verify the validity of the domain name part of the email
  5. return true;
  6. }else{
  7. return false;
  8. }
  9. }else{
  10. return false;
  11. }
  12. }

  13. //Note: The checkdnsrr function is invalid on the win host! Below It is a solution proposed by a foreign programmer. He wrote a function to replace the checkdnsrr function:

  14. function myCheckDNSRR($hostName, $recType=''){
  15. if(!emptyempty($hostName)){
  16. if( $ recType=='' ) $recType="MX";
  17. exec("nslookup -type=$recType $hostName", $result);
  18. foreach($result as $line){
  19. if(eregi("^$hostName ",$line)){
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25. return false;
  26. }

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn