Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP how to get the base domain/url?

Tags:

php

function url(){     if(isset($_SERVER['HTTPS'])){         $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";     }     else{         $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 example.com but it gives me example.com/sub if I'm working in the folder sub. If I'm using the main directory,the function works fine. Is there an alternative to $_SERVER['HTTP_HOST']?

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

like image 202
ZZPLKF Avatar asked Jun 19 '13 20:06

ZZPLKF


People also ask

How do I get the base URL with PHP?

<? php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>

What is the base domain in a URL?

The base domain is the umbrella under which an entire site resides. A base domain consists of only two of the URL elements: Domain Name. Top Level Domain (TLD)


1 Answers

Use SERVER_NAME.

echo $_SERVER['SERVER_NAME']; //Outputs www.example.com 
like image 170
Bad Wolf Avatar answered Oct 03 '22 23:10

Bad Wolf