#native_company# #native_desc#
#native_cta#

Try this one-liner to insure proper path settings.

By jimm pratt
on February 9, 2001

When working on a variety of off-site servers for client websites, often I discover that configurations of the webservers vary so wildly as to cause minor headaches!

One area of annoyance is in using PHP’s $DOCUMENT_ROOT environment variable to insure that paths to the code are handled properly in include/require functions. Different servers implement this variable differently by either adding a slash on the end of the URL or leaving it off. Some older versions of Apache servers seem to have a bug in their mods that put the slash on the end even if configured not to. If you don’t think about checking that configuration/variable before implementing code, you might discover one too many slashes, or no slashes at all, appearing in the url for a link.

Sure, you could use phpinfo() to look up the current configuration of the webserver, or harass the people who installed the server for that information, but with the help of fellow programmer Sven Kleese ([email protected]), we now have a neat little one-liner that can be put in the include/require functions that checks automatically for the presence of the slash, and determines whether or not to add one:

Try this:

include ($DOCUMENT_ROOT . ((preg_match("/[^/]+/$/",$DOCUMENT_ROOT)) ? '' : '/') . 'path_to_your_code/your_code.php');

Although we have not tested it, you should also be able to use getenv(‘DOCUMENT_ROOT’) in place of the $DOCUMENT_ROOT variable. This trick could work with HTML hyperlinks, but instead of using the $DOCUMENT_ROOT variable, you would use $HTTP_HOST and/or $PATH_INFO environment variables, or even your own pre-defined constants.

The above code was tested with Apache on Linux, and Apache on Win32 (ugg – but hey it works!). Your mileage may vary…

Any questions, comments, improvements, or corrections will always be appreciated…

-gremlin ([email protected])