Getting Current host and current folder in PHP

There are times when clients or we start of projects in what i call a development server or a local server or a server that is used for testing. This server can either be local or on the internet.

In development, we are sometimes faced in a situation wherein we would want to provided links that are pre-determined by php so all we have do is to call these links, usually defined as values in variables.

Ex. <a href="<?php echo $homepagelink; ?>">the homepage</a>

To assign a value to the “$homepagelink”, we would be using two built in functions in PHP $_SERVER['HTTP_HOST'] and basename(dirname(__FILE__)). Now don’t take my word for it, because there are other solutions that might work even better and might even be shorter in terms of code.  Anyway here is the short snippet i use for this purpose.


// to get the current domain or the http://www.domain.com/
$domain = $_SERVER['HTTP_HOST'];
// to get the current directory or folder, this is assuming that you are just one folder deep.
$folder=basename(dirname(__FILE__));
$homepagelink= "http://" . $domain ."/".$folder;

So, if you’re host is on say.. http://www.example.com and your folder is named dev, Placing this script inside any php in the folder dev and echo-ing the variable $homepagelink would give us the value of http://www.example.com/dev.  Neat huh?

Related posts:



Services
  • PSD Conversion

    Need a design mockup converted into a functional WordPress theme? We can help! We specialize in slicing and dicing custom mockups into fully functional WordPress themes. The custom designed theme will also utilize all current WordPress features, like m...

  • Wordpress Websites

    Need a WordPress website? We can help! At ETN Multimedia we offer complete website solutions. We put our creativity, originality, and years of expertise to work to design the perfect WordPress website for you. Already have a WordPress website? Ca...

  • Web Design

    Web Design For the budget conscious, we offer basic, simple and static web pages coded in basic HTML, CSS and for a little dynamicity, javascripting. Please feel free to go over our web design packages. We create static web sites based on client ...

  • Web Maintenance Services

    Some corporate websites do not require a major overhaul, but instead require some kind of maintenance with regards to content and basic layout. We are more than qualified in offering regular maintenance and updating services to our clientele. Aside ...

  • Online Application Development

    Dynamism, that is the key on the today's online business, and as anybody can see, that is the future of Internet. If you want create to a dynamic online services like dating site, online shopping platform, social network platform to the World Wide ...

  • Web Development

    People often misunderstand the differences between web development and web design; these are two completely different things. Web development is in charge of the structure of the website, this is the behind the scenes operations, it is where all the c...

  • CMS Solutions

    Modern style editable web solution Having the ability to quickly and easily update your website empowers your organisation to have a highly dynamic online presence. At ETN MultiMedia, we understand that every organisation has different requirements ...

FREE CONSULTATION!
A member of our team will get in touch with you within 24 hours.

Your Name (required)

Land Phone (required)

Mobile Phone

Your Email (required)

Subject (required)

Budget (required)

Your Message

Partners