Sieu Phan Sieu Phan - 2 years ago 127
PHP Question

PHP how to get the base domain/url?

function url(){
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
$protocol = 'http';
return $protocol . "://" . $_SERVER['HTTP_HOST'];

For example with the function above, it works fine if I work with the same directory, but if I make a sub directory, and work in it, it will give me the location of the sub directory also for example. I just want
but it gives me
if I'm working in the folder
. If I'm using the main directory,the function works fine. Is there an alternative to

Or how could I fix my function/code to get the main url only? Thanks.

Answer Source


echo $_SERVER['SERVER_NAME']; //Outputs
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download