Source Rally PHP Community Scripts .. Sign up .. Login
Auto detect root directory - Requires use of .htaccess
Access: Public      Tags: Root Directory, PHP, .htaccess
Add to favourites       Subscribe comments       Copy code       Bookmark
The most common mistake I see in php scripts is directory paths. Most
people don't understand how to reach directories above the current 
location or get back to index without using the index file name. The 
following code provides a variable with a link to the index file.

$uriCount = explode("/", $_SERVER[REQUEST_URI]);
$selfCount = explode("/", $_SERVER[PHP_SELF]);
$depth = count($uriCount) - count($selfCount);
$dirRoot = "";
while ($depth) {
    $dirRoot .= "../";
    $depth--;
}

By using the difference between the uri slash count and the self slash 
count you end up with the depth of the current rewrite directory. This 
only works for a site with a .htaccess path translator but conveys a 
simple process to handle directories. A double dot followed by a slash 
is repeated that many times going a directory higher each time. Follows 
is an example of .htaccess

RewriteEngine On
RewriteRule ^app/([A-Za-z0-9_-]+)$ index.php?app=$1 [L]


This will translate http://www.yoursite.com/app/test into 
http://www.yoursite.com/index.php?app=test. The user never sees that all
directories are processed by a single index.php.
Add to favourites       Subscribe comments       Copy code       Bookmark
Sign up to add your own comment here!

Shared by:

nexuslite

Mail user Add to friends
All user contributed content is available under the LGPL unless specified otherwise.
Remaining copyrights Regin Gaarsmand © 2006-2008
About SourceRally.net
Programador PHP