GetSimple Support Forum

Full Version: php to find the root url
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

We all know that with a php way finding the root path we can use $_SERVER["DOCUMENT_ROOT"]

I am looking for a php way to get the root url of a website (so not with the GS api)

I have not found a clear solution for it on php.net and neither on stackoverflow

This is the best I could find:

// do we have to do with http or with https
$protocol = empty($_SERVER['HTTPS']) ? 'http' : 'https';


// example php code to get the root url for http

$root_url = "http://".$_SERVER['HTTP_HOST'];
$root_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

It works but I am not sure if this is a fool proof way

Does anyone know of a tested and fool proof php way to get the root url
that works for localhost and for online site ?
Look at how I do it in 3.4