PHP how to get the base domain/url?

Solution 1:

Use SERVER_NAME.

echo $_SERVER['SERVER_NAME']; //Outputs www.example.com

Solution 2:

You could use PHP's parse_url() function

function url($url) {
  $result = parse_url($url);
  return $result['scheme']."://".$result['host'];
}