PHP: Get Current Domain Name

Photo of Mike Dalisay
Modified Thursday, January 19, 2012
by - @ninjazhai
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="">
     The Link

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
For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.