Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
php to find the root url
#1
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 ?
Reply
#2
Look at how I do it in 3.4
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply




Users browsing this thread: 1 Guest(s)