Skip to content

Instantly share code, notes, and snippets.

@Aleksandr-ru
Last active October 28, 2016 14:00
Show Gist options
  • Save Aleksandr-ru/4152de3aee0bd2b62b1ec717640b0482 to your computer and use it in GitHub Desktop.
Save Aleksandr-ru/4152de3aee0bd2b62b1ec717640b0482 to your computer and use it in GitHub Desktop.
check host's or url's same origin
<?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