Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yanknudtskov/41a078ee77ce297dedc2 to your computer and use it in GitHub Desktop.
Save yanknudtskov/41a078ee77ce297dedc2 to your computer and use it in GitHub Desktop.
<?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;
}
<?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