2020-12-21, 08:02:22
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 ?
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 ?