Email Domain Matches Site Domain Check
The PHP function below checks the URL specified in a form with the domain specified email address domain. A message is generated when the domains do not match.
This can be used by businesses providing products or services to website owners and wish to receive only serious inquiries. Those with an email address at their own domain are more likely to have serious inquiries.
The "URL" form field may ask for a domain name or a URL. Either way, the domain name is extracted for comparing with the email address. Leading "www." in domains names are ignored during checking. But other subdomains are considered part of the domain name.
The PHP code contains the function that does the check and an example handling of any messages the function generates.
<?php function EmailAndDomainMatchError() { // Returns empty string if match is okay. // Otherwise, an error message. // The form field names for comparing. $EmailFormFieldName = 'email'; $URLdomainFormFieldName = 'URL'; // Check both fields contain information. if( empty($_POST[$URLdomainFormFieldName]) or empty($_POST[$EmailFormFieldName]) ) { return 'Both Email and URL/domain must be provided.'; } // Extract domain name from email address. $addypieces = explode('@',$_POST[$EmailFormFieldName]); $emailname = strtolower(trim($addypieces[0])); $emaildomain = strtolower(trim($addypieces[1])); // Some email address error checking. if( count($addypieces)!=2 or strpos($_POST[$EmailFormFieldName],',') or (!strpos($_POST[$EmailFormFieldName],'.')) ) { return 'Incorrect Email.'; } // Extract domain name from URL. $domain = preg_replace('/^https?:\/\//i','',$_POST[$URLdomainFormFieldName]); $domain = preg_replace('/^www\.?/i','',$domain); $domain = preg_replace('/\/.*$/','',$domain); $domain = strtolower(trim($domain)); // Some domain name error checking. if( empty($domain) or strpos($domain,',') or (!strpos($domain,'.')) ) { return 'Incorrect Domain.'; } // Validate match. if( ! preg_match("/$emaildomain$/",$domain) ) { return 'Email domain must be same as URL domain. Example: '.$emailname.'@'.$domain; } return ''; } $Error = EmailAndDomainMatchError(); if( $Error ) { echo "<h3>$Error</h3>"; } ?>
At lines 7 and 8 of the above code, specify the form field names where the URL/domain and the email address are provided.
The handling of any messages generated by the function can also be customized.
Will Bontrager