Sunday 15 July 2012

PHP defining document root on badly configured shared server -


I have an account on a shared server that is no longer kept and it is not very well configured. $ _SERVER ["SCRIPT_FILENAME"] = '/path/to/my/root/and/now/path/to/my/file/location.php' echo < / Pre>

but ...

  $ _SERVER echo ["DOCUMENT_ROOT"] = '/ var / www'   

Finding the site root Was needed, so I have written my own method to do this, what am I thinking is that anyone is showing any reason, because before I will do it before by putting it into production?

Note: I know that it is not included in / is required, the URL is rewritten or will not work with index.php or any other root extension is configured to access Apache quietly has been done.

My method:

  define ('ROOT_PATH', str_replace (str_replace ('/'. BaseName (__ file__), '', $ _SERVER ["SCRIPT_URL" ]), '', Dirname (__ FILE__))); Need_once ROOT_PATH '/ And / now / path / in / my / file / location.php';   

In short, this script eliminates the name of the script from the URL, then removes any directory name found after the root.

EDIT: My description was said to be 'after root' above, but the str_replace was not taken into consideration in the string where the replacement occurred. It could break it, so I changed the last example of the matching path just to modify my solution:

  define ('ROOT_PATH', 'preg_replace' (. * *) '.' Preg_quote (str_replace ('/' ('_'), '$ 1'. ''. '$ 2', dirname (__ file __), 1));   

preg_replace is unfortunately less slow.

The best approach is that Avoid using all of them and define 'App Root' instead. If your index.php is in this file , Then you just need:

  define ('APP_ROOT', dirname (__ FILE__));   

If you instead use some included file To define it, for example, call in / passot.php , just call dirname each subfolder:

  define ('APP_ROOT', dirname (dirname (__ FILE__));   

This is not useful for knowing the root cause of the document.

No comments:

Post a Comment