PDA

View Full Version : open_basedir restriction in effect error in PHP



BenR41
25 Feb 2008, 09:44 AM
This must be one of the most annoying errors to get when working with php. This error occurs when PHP can't find the specified file in the include statement. This often occurs when a developer has built an application on their machine and uploaded the application to a shared hosting account or if they move the application to a sub folder.

An easy work around is to determine the path to the called file dynamically.


$folcnt = substr_count($_SERVER["PHP_SELF"], "/");
$folappstep = "";
if($folcnt > 1) {
for($i=2;$i<=$folcnt;$i++) {
$folappstep .= "../";
}
} else {
$folappstep = "";
}

$extfile = $folappstep."ws8_extensions.php";
$funfile = $folappstep."ws8_functions.php";

include $extfile;
include $funfile;

Thanks

Ben
TS Media Ltd.
[link deleted]

paulkoan
27 Feb 2008, 06:41 PM
Hi Ben,

I have looked at this snippet a bunch of times, and I am trying to understand how it would be used.

So lets say our PHP_SELF is

/this/is/my/script.php

Folcnt contains the nesting depth: 4

Then we build a string that contains "../" x folcnt. Incidentally, you could do this with


str_repeat("../",$folcnt-1)


And the "else" part of the isn't necessary as you already set $folappstep to "" before deciding whether you need to run the for loop.

Anyway, it has dawned on me why you'd want to do this. You want to pull the required files from the root of your webspace, whereas "require" works from the root of the file system.

Here is your code with the simplifications I mentioned:


$folcnt = substr_count($_SERVER["PHP_SELF"], "/");
$folappstep = str_repeat("../",$folcnt-1);

$extfile = $folappstep."ws8_extensions.php";
$funfile = $folappstep."ws8_functions.php";

include $extfile;
include $funfile;


However, what you want is for your scripts to be aware of the document root, and pull their included files from there. Consider this:



ini_set("include_path", getenv("DOCUMENT_ROOT"));
include "ws8_extensions.php";
include "ws8_functions.php";


What this is doing is adding the document root for your website to the include search path, and then you can include the files directly without referencing their location.

Cheers,

Paul Murphy
ServWise Advanced Hosting - Better, Faster, Smarter