PHP: Get Current Domain Name

PHP Quick Tip: I used this code when I was suddenly just got into a project and too lazy to meet and greet its configuration file to see how the current domain name was defined. I had to provide a link in a page but I don’t want it to be hard coded like this:

<a href=“http://somehardcodeddomain.com/users/add/”>
     The Link
</a>

This is because there are instances when the client wanted to change their domain name. So if that happens, to prevent re-coding links like that, we have to get current domain name programatically:

<a href=”http://<?php echo $_SERVER['HTTP_HOST']; ?>/users/add/”>
     The Link
</a>

Hi! I'm Mike Dalisay, the co-founder of codeofaninja.com, a site that helps you build web applications with PHP and JavaScript. Need support? Comment below or contact [email protected]

I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on LinkedIn, Twitter, Facebook, and Instagram.