I have an application that will run on other domains. The application needs to check if an email address matches the domain of the site running the application.
So, if email = [email protected] ,
- if application is running on example.com , return true
- if application is running on anything.example.com, return true
- if application is running on domain.com, return false
I understand that the $_SERVER variables might be incorrect due to spoofing. So what is the best way for the server running the application to get the server's domain name?
My Answer:
@example.com and the current domain is example.com, the application should return true. If the email is [email protected] and the current domain is example.com, the application should return false.
Here is a PHP function that can be used to check if the email domain matches the current domain:
php
function emailDomainMatchesCurrentDomain($email) {
$emailParts = explode('@', $email);
$emailDomain = $emailParts[1];
$currentDomain = $_SERVER['HTTP_HOST'];
return $emailDomain === $currentDomain;
}
$email = 'example@example.com';
if (emailDomainMatchesCurrentDomain($email)) {
echo 'Email domain matches current domain';
} else {
echo 'Email domain does not match current domain';
}
This function first splits the email address into two parts using the "@" symbol. It then compares the domain part of the email address with the current domain obtained from the $_SERVER['HTTP_HOST'] variable. If the two domains match, the function returns true, indicating that the email domain matches the current domain. Otherwise, it returns false.
Rate this post
3 of 5 based on 2723 votesComments