Last active
October 28, 2016 14:00
-
-
Save Aleksandr-ru/4152de3aee0bd2b62b1ec717640b0482 to your computer and use it in GitHub Desktop.
check host's or url's same origin
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 sameOrigin($url1, $url2, $level = 2) | |
{ | |
$host1 = strpos($url1, '://') ? parse_url($url1, PHP_URL_HOST) : $url1; | |
$host2 = strpos($url2, '://') ? parse_url($url2, PHP_URL_HOST) : $url2; | |
return host2origin($host1, $level) == host2origin($host2, $level); | |
} | |
function host2origin($host, $level = 2) | |
{ | |
$host_arr = explode('.', $host); | |
while(count($host_arr) > $level) array_shift($host_arr); | |
return implode('.', $host_arr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment