3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function is_valid_time_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; } 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 ) ) ) { return true; break; } } return false; } $domains = array( 'http://mydomain.com', 'http://www.mydomain.com', 'http://mydomain.com.evilsite.com', 'http://mydomain.com.mydomain.net', 'http://evilsitemydomain.com', 'http://mydomain.com.evil%45site.com', 'http://evil%45sitemydomain.com', 'http://evil%00sitemydomain.com', 'http://mydomain.com.evil%00site.com','http://evilsite.com/.mydomain.com', 'http://mydomain.com%00.evilsite.com', 'http://evilsite.com#.mydomain.com', 'http://evilsite.com@mydomain.com', 'http://mydomain.com@evilsite.com', 'http://evilsite.com#@mydomain.com', 'http://evilsite.com:mydomain.com', 'file://stuff@myfile:mydomain.net', 'file:///dev@mydomain.net', 'file:///dev?@mydomain.net', 'file:///dev?mydomain.net', 'http:mydomain.net', 'http:evilsite.com//mydomain.net'); foreach( $domains as $domain ) { echo $domain . "\n"; var_dump( is_valid_time_domain( $domain ) ); }
based on eT2Ft
Output for 5.3.3 - 7.2.0
http://mydomain.com bool(true) http://www.mydomain.com bool(true) http://mydomain.com.evilsite.com bool(false) http://mydomain.com.mydomain.net bool(true) http://evilsitemydomain.com bool(false) http://mydomain.com.evil%45site.com bool(false) http://evil%45sitemydomain.com bool(false) http://evil%00sitemydomain.com bool(false) http://mydomain.com.evil%00site.com bool(false) http://evilsite.com/.mydomain.com bool(false) http://mydomain.com%00.evilsite.com bool(false) http://evilsite.com#.mydomain.com bool(false) http://evilsite.com@mydomain.com bool(true) http://mydomain.com@evilsite.com bool(false) http://evilsite.com#@mydomain.com bool(false) http://evilsite.com:mydomain.com bool(false) file://stuff@myfile:mydomain.net bool(false) file:///dev@mydomain.net bool(false) file:///dev?@mydomain.net bool(false) file:///dev?mydomain.net bool(false) http:mydomain.net bool(false) http:evilsite.com//mydomain.net bool(false)
Output for 5.2.0 - 5.3.2
http://mydomain.com bool(true) http://www.mydomain.com bool(true) http://mydomain.com.evilsite.com bool(false) http://mydomain.com.mydomain.net bool(true) http://evilsitemydomain.com bool(false) http://mydomain.com.evil%45site.com bool(false) http://evil%45sitemydomain.com bool(false) http://evil%00sitemydomain.com bool(false) http://mydomain.com.evil%00site.com bool(false) http://evilsite.com/.mydomain.com bool(false) http://mydomain.com%00.evilsite.com bool(false) http://evilsite.com#.mydomain.com bool(false) http://evilsite.com@mydomain.com bool(true) http://mydomain.com@evilsite.com bool(false) http://evilsite.com#@mydomain.com bool(false) http://evilsite.com:mydomain.com Warning: parse_url(http://evilsite.com:mydomain.com): Unable to parse URL in /in/dYLWg on line 4 bool(false) file://stuff@myfile:mydomain.net Warning: parse_url(file://stuff@myfile:mydomain.net): Unable to parse URL in /in/dYLWg on line 4 bool(false) file:///dev@mydomain.net bool(false) file:///dev?@mydomain.net bool(false) file:///dev?mydomain.net bool(false) http:mydomain.net bool(false) http:evilsite.com//mydomain.net bool(false)
Output for 5.1.2 - 5.1.6
http://mydomain.com bool(true) http://www.mydomain.com bool(true) http://mydomain.com.evilsite.com bool(false) http://mydomain.com.mydomain.net bool(true) http://evilsitemydomain.com bool(false) http://mydomain.com.evil%45site.com bool(false) http://evil%45sitemydomain.com bool(false) http://evil%00sitemydomain.com bool(false) http://mydomain.com.evil%00site.com bool(false) http://evilsite.com/.mydomain.com bool(false) http://mydomain.com%00.evilsite.com bool(false) http://evilsite.com#.mydomain.com bool(false) http://evilsite.com@mydomain.com bool(true) http://mydomain.com@evilsite.com bool(false) http://evilsite.com#@mydomain.com bool(false) http://evilsite.com:mydomain.com Warning: parse_url(http://evilsite.com:mydomain.com): Unable to parse url in /in/dYLWg on line 4 bool(false) file://stuff@myfile:mydomain.net Warning: parse_url(file://stuff@myfile:mydomain.net): Unable to parse url in /in/dYLWg on line 4 bool(false) file:///dev@mydomain.net bool(false) file:///dev?@mydomain.net bool(false) file:///dev?mydomain.net bool(false) http:mydomain.net bool(false) http:evilsite.com//mydomain.net bool(false)
Output for 4.3.0 - 5.1.1
http://mydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://www.mydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://mydomain.com.evilsite.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://mydomain.com.mydomain.net Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://evilsitemydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://mydomain.com.evil%45site.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://evil%45sitemydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://evil%00sitemydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://mydomain.com.evil%00site.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://evilsite.com/.mydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://mydomain.com%00.evilsite.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://evilsite.com#.mydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://evilsite.com@mydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://mydomain.com@evilsite.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://evilsite.com#@mydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http://evilsite.com:mydomain.com Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) file://stuff@myfile:mydomain.net Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) file:///dev@mydomain.net Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) file:///dev?@mydomain.net Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) file:///dev?mydomain.net Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http:mydomain.net Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false) http:evilsite.com//mydomain.net Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/dYLWg on line 4 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dYLWg on line 4 bool(false)