Created
January 17, 2012 00:27
-
-
Save mjangda/1623788 to your computer and use it in GitHub Desktop.
Checks if a given url matches a domain whitelist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function my_is_valid_domain( $url ) { | |
$whitelisted_domains = array( 'mydomain.com', 'mydomain.net' ); | |
$domain = parse_url( $url, PHP_URL_HOST ); | |
// Check if we match the domain exactly | |
if ( in_array( $domain, $whitelisted_domains ) ) | |
return true; | |
$valid = false; | |
foreach( $whitelisted_domains as $whitelisted_domain ) { | |
$whitelisted_domain = '.' . $whitelisted_domain; // Prevent things like 'evilsitetime.com' | |
if( strpos( $domain, $whitelisted_domain ) === ( strlen( $domain ) - strlen( $whitelisted_domain ) ) ) { | |
$valid = true; | |
break; | |
} | |
} | |
return $valid; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$domains = array( 'http://mydomain.com', 'http://www.mydomain.com', 'http://mydomain.com.evilsite.com', 'http://mydomain.com.mydomain.net', 'http://evilsitemydomain.com' ); | |
foreach( $domains as $domain ) { | |
echo $domain . "\n"; | |
var_dump( is_valid_time_domain( $domain ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, just what I need. A couple comments:
So test.php line 6 is:
var_dump( my_is_valid_domain( strtolower( $domain ) ) );