Skip to content

Instantly share code, notes, and snippets.

@rissajeanne
Created April 11, 2012 17:53
Show Gist options
  • Save rissajeanne/2360908 to your computer and use it in GitHub Desktop.
Save rissajeanne/2360908 to your computer and use it in GitHub Desktop.
httpRefererMatchesBaseUrl()
<?php
private function httpRefererMatchesBaseUrl() {
if (!array_key_exists('HTTP_REFERER', $_SERVER) ||
!get_instance()->config->item('base_url')) {
return false;
}
$baseUrl = parse_url(get_instance()->config->item('base_url'));
$referer = parse_url($_SERVER['HTTP_REFERER']);
$cleanHttpReferer = $referer['host'];
$cleanBaseUrl = $baseUrl['host'];
// Strip the "m." prefix, if it exists
if (stripos($cleanBaseUrl, 'm.') === 0) {
$cleanBaseUrl = substr($cleanBaseUrl, 2);
}
if (stripos($cleanHttpReferer, 'm.') === 0) {
$cleanHttpReferer = substr($cleanHttpReferer, 2);
}
// Strip the lang subdomain, if it exists
if (stripos($cleanBaseUrl, '.') === 2) {
$cleanBaseUrl = substr($cleanBaseUrl, 3);
}
if (stripos($cleanHttpReferer, '.') === 2) {
$cleanHttpReferer = substr($cleanHttpReferer, 3);
}
return ($cleanHttpReferer === $cleanBaseUrl);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment