For those of you who have been programming in PHP much at all, you will be familiar with the $_GET and $_POST variables that can be passed to your scripts. You may not be as familiar with the $_SERVER variable that gives you information about your host environment.
I often use the $_SERVER variable to get the domain name of the site on which my php scripts are installed. That way, I can move my code to a totally different server and not have to go through and change a lot of the code.
To find my current domain name I can use this:
$imagesurl = 'http://'.$_SERVER['HTTP_HOST'].'/images';
If I were on my ‘cindycullen.com’ website, $imagesurl would be set to this:
http://cindycullen.com/images
Another I use quite often is $_SERVER[‘REQUEST_URI’]. This will give me the url path without the domain name. For instance, if the URL that was used to request my current webpage was something like this:
http://cindycullen.com/path/to/file.php
then using this:
echo $_SERVER['REQUEST_URI'];
would give me this:
/path/to/file.php
Not all Servers will have the same $_SERVER variables. It will depend on how your server is setup. To find out which $_SERVER variables you have available on your server, you can do this:
print_r($_SERVER);
That will all run together in your browser, but if you view source for the page, the array of variables should be much easier to read!