Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Created March 11, 2012 04:34
Show Gist options
  • Save jehoshua02/2015043 to your computer and use it in GitHub Desktop.
Save jehoshua02/2015043 to your computer and use it in GitHub Desktop.
Determine url base reliably
<?php
class Controller {
private $_base_url;
// ...
public function base_url()
{
if (empty($this->_base_url))
{
$https = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off');
$protocol = ($https) ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$path = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$this->_base_url = $protocol . '://' . $host . $path;
}
return $this->_base_url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment