Results 1 to 2 of 2

Thread: open_basedir restriction in effect error in PHP

  1. #1
    Join Date
    Feb 2008
    Posts
    3

    open_basedir restriction in effect error in PHP

    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]
    Last edited by alvo; 06 May 2008 at 02:09 PM. Reason: New users aren't allowed links/signatures.

  2. #2
    Join Date
    Feb 2008
    Posts
    85
    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
    Code:
    /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
    Code:
    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:

    Code:
    $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:

    Code:
    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

Similar Threads

  1. CMS advice (php)
    By Littlened in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 07 Jul 2007, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •