Background
Most web developers realize quickly that they should use relative URL's on their web pages when referring to anything on the same web site (anchors, images, CSS file paths, Javascript file paths, etc). It's a good way of letting an entire web site function properly without having to know its own URL.
If the company changes domain names from "foobarcorp.com" to "foobar.com", nothing needs to be changed. If they have 3 domain names all pointing to the same web site, they all work fine.
Usually.
There are a few special case situations that can arise, though, when your PHP code must know more.
Special Situations
Your code might need to know what server it's on (your development server, your test server, or the production server); for example, when setting the file upload path for web forms where the user can upload files. That generally should be an absolute Unix path, and you probably want dev and test uploads to be separated from production uploads, so we need to detect what type of server we're on.
Another time you need to know which server you're on is with Google Maps API, such as when you're embedding a live Google Map on a web page. Because for that to work, you must have generated a Google API Key, which will only work with a single URL! This means you'll need at least 3 different keys (development URL, test URL, production URL).
Your Server Arrangement
Different web developers have different philosophies on how to arrange the dev/test/prod servers. For simplicity's sake I'll give you an example setup which isn't quite how I operate, but would work just fine.
Let's decide that the Dev and Test servers are at these URLs:
dev.somecompany.mydomain.com test.somecompany.mydomain.com |
Where "mydomain.com" is my own web site, as the web developer.
And of course, the company has their own URL for their production web site:
www.somecompany.com |
Now let's say that you set up the DOCUMENT_ROOTs to be:
/home/somecompany/dev/ /home/somecompany/test/ /home/somecompany/prod/ |
Detecting Server Type
The PHP code to detect what server you're on is very simple. Here we obtain the name of the area: "dev", "test", or "prod" from the URL.
$parts = array_slice(split('/',$_SERVER['SCRIPT_FILENAME']),0,4);
$area = $parts[3];
|
In the above example I used the $_SERVER['SCRIPT_FILENAME'] variable, which yields the absolute Unix path to the currently running PHP script, i.e.
/home/somecompany/test/index.php |
Generating Upload Paths
Another situation is when you need to let your web-visitor upload a file of some sort. You want to place the file in a directory or folder somewhere.
The code you write for this is easy. Here's a chunk that extracts the first 3 elements between slashes, and re-combines it, adding the picture directory to the end.
$pictureDir = "/img/pictures"; // relative to DOCUMENT_ROOT
$parts = array_slice(split('/',$_SERVER['SCRIPT_FILENAME']),0,3);
$config['upload_path'] = join('/',$parts) . $pictureDir;
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['encrypt_name'] = TRUE; // pick unique but weird filenames
|
In this example, $pictureDir is the path to where we want the uploaded pictures to be stored, beneath DOCUMENT_ROOT. But we have to specify the full path, not relative, when uploading a file! So we must prepend the right DOCUMENT_ROOT on the front of $pictureDir.
The array_slice() call extracts the area name ("dev","test","prod"), and the join() call put the first 3 pieces back together again - which is the right DOCUMENT_ROOT.
Note: I'm using the CodeIgniter framework here, the $config array is a requirement for CodeIgniter's Upload library.
Other Advantages
This has the added advantage that if you ever move this web site to another server, so the DOCUMENT_ROOT path changes, it will still work - we don't assuming anything about where our files are located.
Adding this kind of flexibility up front helps alleviate debugging and other problems when things change in the future. It's much easier to write code like this (and debug it) when this part of the project is fresh in your mind. It's much more difficult a few months or years from now, especially if you have to move dozens of web sites that all have hard-coded paths in them, to a new server.
Don't miss the latest web tips and tricks!
Subscribe to our low-volume mailing list:
Privacy Policy
| Copyright © 2006 Fastech Learning LLC, all rights reserved. |
| Phone toll free 1-866-464-6688, Phoenix Metro area 480-895-6688 |
| Problem with this web site? please let us know |