Laravel: Check for email service provider domain and it subdomain - php

I wants restrict user to not to add some popular email service provider domain like gmail.com, yahoo.com, ymail.com, hotmail.com
for that I have created an array
$invalidDomain = ['gmail.com', 'yahoo.com', 'ymail.com', 'hotmail.com']
and then check user input with in_array
if(in_array($insertDomain, $invalidDomain)){
//restrict
}
but now I also want to check for gmail.co.in, hotmail.co.uk
how can I?

You can use regular expressions to achieve this - it will give you more flexibility eg: if you would like to exclude gmail.co.uk but allow gmail.com. See the code snippet below:
$insertDomain = "gmail.com";
$invalidDomain = ['gmail\.[a-zA-Z\.]{2,}', 'yahoo\.[a-zA-Z\.]{2,}', 'ymail\.[a-zA-Z\.]{2,}', 'hotmail\.[a-zA-Z\.]{2,}'];
// join regexp
if (preg_match('/^'.implode("$|^", $invalidDomain).'$/', $insertDomain)) {
// restrict
echo $insertDomain."\n";
}

Used this type of code
$invalidDomain = ['gmail', 'yahoo', 'ymail', 'hotmail']
and finally in the condition
//$insertDomain = "gmail";
if(in_array($insertDomain, $invalidDomain)){
//restrict
}

You could use PHP's parse_url to determine what domain is being used http://php.net/manual/en/function.parse-url.php
The output array would contain host index that would give you the domain / sub-domain used within the string you pass as an argument to that function .i.e.
$partials = parse_url('https://google.com/?id=123');
$insertDomain = $partials['host']; // google.com

Related

Stuck with REQUST_URI Parsing ( PHP )

I have a page working as I need it to, with the last /arist-name/ parsing into the correct variable, but the client is adding /artist-name/?google-tracking=1234fad to their links, which is breaking it.
http://www.odonwagnergallery.com/artist/pierre-coupey/ WORKS
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID] DOES NOT WORK
$expl = explode("/",$_SERVER["REQUEST_URI"]);
$ArtistURL = $expl[count($expl)-1];
$ArtistURL = preg_replace('/[^a-z,-.]/', '', $ArtistURL);
Please help, I have been searching for a solution. Thanks so much!
PHP has a function called parse_url which should clean up the request uri for you before you try to use it.
parse_url
Parse a URL and return its components
http://php.net/parse_url
Example:
// This
$url_array = parse_url('/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]');
print_r($url_array);
// Outputs this
Array
(
[path] => /artist/pierre-coupey/
[query] => mc_cid=b7e918fce5&mc_eid=[UNIQID]
)
Here is a demo: https://eval.in/873699
Then you can use the path piece to perform your existing logic.
If all your URLs are http://DOMAIN/artist/SOMEARTIST/
you could do:
$ArtistURL = preg_replace('/.*\/artist\/(.*)\/.*/','$1',"http://www.odonwagnergallery.com/artist/pierre-coupey/oij");
It would work in this context. Specify other possible scenarios if there are others. But #neuromatter answer is more generic, +1.
if you simply want to remove any and all query parameters, this single line would suffice:
$url=explode("?",$url)[0];
this would turn
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
into
http://www.odonwagnergallery.com/artist/pierre-coupey/
but if you want to specifically remove any mc_cid and mc_eid parameters, but otherwise keep the url intact:
$url=explode("?",$url);
if(count($url)===2){
parse_str($url[1],$tmp);
unset($tmp['mc_cid']);
unset($tmp['mc_eid']);
$url=$url[0].(empty($tmp)? '':('?'.http_build_query($tmp)));
}else if(count($url)===1){
$url=$url[0];
}else{
throw new \LogicException('malformed url!');
}
this would turn
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
into
http://www.odonwagnergallery.com/artist/pierre-coupey/?anything_else=whatever

How to validate email address by domain using PHP?

I want to validate the e-mail domain using php, because some users trying to submit contact form using dummy email ids like: aa#bb.com
TRY with checkdnsrr extract the domain name from the email address and pass to the checkdnsrr.
Returns TRUE if domain name are found; returns FALSE if no domain name
were found or if an error occurred.
$domainname = "domain.com";
checkdnsrr($domainname , "A");
If you only want to validate the syntax of the domain name section you could split the email on the # and run this regex on the second part (after running it through the idn_to_ascii() function for intenational domain names):
/
^
(?(DEFINE)(?<part>(?:xn--)?[a-z\d](?:[a-z\d-]*[a-z\d])?))
(?(DEFINE)(?<subpart>(?:xn--)?[a-z\d_](?:[a-z\d_-]*[a-z\d])?))
(?:(?&subpart)\.)*
(?&part)
(?:\.[a-z]+|\.xn--[a-z\d]+){1,2}
$
/xigm
https://regex101.com/library/PAKVdK
You will need to check if there is a MX record for that domain.
Consider this script in addition to you regex validation
https://davidwalsh.name/php-email-validator
Be aware that this will not 'really' validate emails completely. User could be invalid.
If you don't want to go through all the hassle of doing the validations by yourself, just use the free API plan from MailboxValidator.
They have some sample codes to help you with integration.
http://www.mailboxvalidator.com/api-single-validation
<?php
$apiKey = 'Enter_License_Key';
$params['format'] = 'json';
$params['email'] = 'Enter_Email';
$query = '';
foreach($params as $key=>$value){
$query .= '&' . $key . '=' . rawurlencode($value);
}
$try = 0;
do {
////////////
//For https request, please make sure you have enabled php_openssl.dll extension.
//
//How to enable https
//- Uncomment ;extension=php_openssl.dll by removing the semicolon in your php.ini, and restart the apache.
//
//In case you have difficulty to modify the php.ini, you can always make the http request instead of https.
////////////
$result = file_get_contents('https://api.mailboxvalidator.com/v1/validation/single?key=' . $apiKey . $query);
} while(!$result && $rty++ < 3);
$data = json_decode($result);
print_r($data);
?>

Display different content depending on Referrer

Hey I am trying to display a different phone number for visitors my website from my Google adwords campaign.
The code below works without the else statement (so if I click through to the page from Google it will display a message, and if I visit the site regularly it does not). When I added the else statement it outputs both numbers. Thank you
<?php
// The domain list.
$domains = Array('googleadservices.com', 'google.com');
$url_info = parse_url($_SERVER['HTTP_REFERER']);
if (isset($url_info['host'])) {
foreach($domains as $domain) {
if (substr($url_info['host'], -strlen($domain)) == $domain) {
// GOOGLE NUMBER HERE
echo ('1234');
}
// REGULAR NUMBER HERE
else {
echo ('12345');
}
}
}
?>
Your logic is slightly skewed; you're checking to see if the URL from parse_url matches the domains in your array; but you're running through the whole array each time. So you get both a match and a non-match, because google.com matches one entry but not the other.
I'd suggest making your domains array into an associative array:
$domains = Array('googleadservices.com' => '1234',
'google.com' => '12345' );
Then you just need to check once:
if (isset($url_info['host'])) {
if (isset($domains[$url_info['host']])) {
echo $domains[$url_info['host']];
}
}
I've not tested this, but it should be enough for you to see the logic.
(I've also removed the substr check - you may need to put that back in, to ensure that you're getting the exact string that you need to look for)

How to restrict public email id for registration in PHP?

I have a registration form that uses any kind of emails for registration. I want to restrict it to company mail id's only. In other words, no free email service provider's mail id would work for registration.
How about a white/black list of domains like the following:
$domainWhitelist = ['companydomain.org', 'companydomain.com'];
$domainBlacklist = ['gmail.com', 'hotmail.com'];
$domain = array_pop(explode('#', $email));
//white list
if(in_array($domain, $domainWhitelist)) {
//allowed
}
//black list
if(!in_array($domain, $domainBlacklist)) {
//allowed
}
Since you have not provided any additional information as to how E-mail addresses are being defined and/or entered into a form or not, am submitting the following using PHP's preg_match() function, along with b and i pattern delimiters and an array.
b - word boundary
i - case insensitive
http://php.net/manual/en/function.preg-match.php
The following will match against "gmail" or "Gmail" etc. should someone want to trick the system.
Including Hotmail, Yahoo. You can add to the array.
<?php
$_POST['email'] = "email#Gmail.com";
$data = $_POST['email'];
if(preg_match("/\b(hotmail|gmail|yahoo)\b/i", $data)){
echo " Found free Email service.";
exit;
}
else{
echo "No match found for free Email service.";
exit;
}
Actually, you can use:
if(preg_match("/(hotmail|gmail|yahoo)/i", $data))
instead of:
if(preg_match("/\b(hotmail|gmail|yahoo)\b/i", $data))
which gave the same results.

php validate email address based on the domain name

I need to filter some email address based on the domain name :
Basically if the domain name is yahoo-inc.com, facebook.com, baboo.com .. (and a few others) the function should do something and if the domain is different it should do something else .
The only way I know to do this is to use a pattern/regex with preg_match_all and create cases/conditions for each balcklisted domain (e.g if domain = yahoo-inc) do this elseif (domain == facebook.com ) do this ... etc but I need to know if there is a more simple/concis way to include all the domains that I want to filter in a single variable/array and then apply only 2 conditions (e.g if email is in black list {do something } else {do something else}
Extract the domain portion (i.e. everything after the last '#'), down case it, and then use in_array to check whether it's in your blacklist:
$blacklist = array('yahoo-inc.com', 'facebook.com', ...);
if (in_array($domain, $blacklist)) {
// bad domain
} else {
// good domain
}
Adding on to #Alnitak here is the full code to do what you need
$domain = explode("#", $emailAddress);
$domain = $domain[(count($domain)-1)];
$blacklist = array('yahoo-inc.com', 'facebook.com', ...);
if (in_array($domain, $blacklist)) {
// bad domain
} else {
// good domain
}
Well here's a very simple way to do this, a VALID email address should only ever contain a single # symbol, so aslong as it validation you can just explode the string by # and collect the second segment.
Example:
if (filter_var($user_email, FILTER_VALIDATE_EMAIL))
{
//Valid Email:
$parts = explode("#",$user_email);
/*
* You may want to use in_array if you already have a compiled array
* The switch statement is mainly used to show visually the check.
*/
switch(strtolower($parts[1]))
{
case 'facebook.com':
case 'gmail.com':
case 'googlemail.com':
//Do Something
break;
default:
//Do something else
break;
}
}

Categories