3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function urlWhitelisted($url) { $always_allowed = array('localhost'); $whitelisted_domains = array_merge(array('fxnetworks.com'), $always_allowed); var_dump($whitelisted_domains); if (strpos($url, "/") !== 0 && strpos($url, "http") !== 0) { $url = 'http://' . $url; } $domain = parse_url($url, PHP_URL_HOST); var_dump($domain); if (strpos($url, "/") === 0 || in_array($domain, $whitelisted_domains)) { return true; } foreach ($whitelisted_domains as $whitelisted_domain) { $whitelisted_domain = '.' . $whitelisted_domain; if (strpos($domain, $whitelisted_domain) === (strlen($domain) - strlen($whitelisted_domain))) { return true; } } return false; } $domains = array('/user', 'localhost/user', 'http://localhost:4039', 'http://fxnetworks.com/user', 'http://google.com', 'google.com', 'fxnetworks.com.google.com'); foreach($domains as $domain) { var_dump(urlWhitelisted($domain)); }
Output for 5.1.2 - 7.1.0
array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } NULL bool(true) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } string(9) "localhost" bool(true) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } string(9) "localhost" bool(true) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } string(14) "fxnetworks.com" bool(true) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } string(10) "google.com" bool(false) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } string(10) "google.com" bool(false) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } string(25) "fxnetworks.com.google.com" bool(false)
Output for 4.3.0 - 5.1.1
array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/fUdAZ on line 12 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/fUdAZ on line 12 NULL bool(true) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/fUdAZ on line 12 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/fUdAZ on line 12 NULL bool(false) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/fUdAZ on line 12 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/fUdAZ on line 12 NULL bool(false) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/fUdAZ on line 12 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/fUdAZ on line 12 NULL bool(false) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/fUdAZ on line 12 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/fUdAZ on line 12 NULL bool(false) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/fUdAZ on line 12 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/fUdAZ on line 12 NULL bool(false) array(2) { [0]=> string(14) "fxnetworks.com" [1]=> string(9) "localhost" } Notice: Use of undefined constant PHP_URL_HOST - assumed 'PHP_URL_HOST' in /in/fUdAZ on line 12 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/fUdAZ on line 12 NULL bool(false)