I'm hosting on a friends server and he has compliled PHP with vertual directories off (I believe that is the correct way to put it).

The problem is that I can't call files with include() or with opendir(), etc., with root relative paths such as include('/folder/foo.php');

We have solved this for the include() function by setting an include path in the .ini file, but this has limitations and does nothing for other functions apparently, and I don't see that we can configure paths for other functions at .ini, nor do I want to do this for every function I use.

Is there a way, short of recompiling, to set a root relative path for all php functions?

Thanks in advance!

    I'm doing something like:
    $currentDir = getcwd(); // dir THIS script is in
    // which turns out to be /home/thisdomain/www/web/
    $currentHome = substr($currentDir, 0, strpos($currentDir, "/www/"));
    // which is /home/thisdomain
    $libDir = currentHome . "/www/web/lib/";
    include $libDir . "lib1.php";

    Would be sort of nice to wrap the above into a function in a lib. 🙂

      Thanks azkworld!

      Not exactly what I'm looking for, but on that topic it would then seem that you need to leave that variable definition at the top of each page, no?

      I guess I may be looking for a way to turn on virtual directories without recompiling. Or some solution that allows for root relative paths in my PHP functions:

      opendir('/foo/bar.php');

        Correct, on top of each page.
        Also correct, not exactly what you were looking for.
        Unfortunately, we have found no way around it at all -- root-relative although technically worked, but caused too many problems migrating, etc. As an ISP, we have several servers with different paths (that is, different methodologies of assigning roots and virtual roots) and moving client sites from dev to production server was sometimes causing problems. Additionally, we have a couple of very secure sites, and one of the protection algorithms they use are the auto-migrating scripts, where sometimes the scripts to be included are taken outside the server root and then symlinked back. The method I mentioned works in all cases. Basically, the only lib you include using this method is a lib with one function we wrote called "trans_path($path)", which we then use everywhere else.
        Oh, well.

          Great, thanks!

          No, not what I was looking for, but very helpful in clearly helping me to "not go there" with other time waisters!

          Could you clarify this bit

          Basically, the only lib you include using this method is a lib with one function we wrote called "trans_path($path)", which we then use everywhere else.

          elaboration would be greatfully excepted! 🙂

            we use this bit to include the lib called dir_lib.php
            that lib has one f in it, called trans_path
            so even if we have to unclude a second lib in the same page, we already do
            include trans_path("lib2.php");
            which does the same thing as above many lines of code to the next lib.
            it also takes care of preventing recursive includes, such as:
            a.php: include b.php
            c.php: include a.php; include b.php;

            as you can see, c.php would generate an error as a already includes b, so when parsing c will get to point of including b, it will generate a number of errors saying functions (vars, etc) are already declared.
            to prevent this, each lib is started like this:
            lib1.php:
            $lib1_included = true;

            other lib1 stuff

            So the actual include lines are:
            if(!$lib1_included) include trans_path("lib1.php");

            furthermore, we found ourselves in a situation where we had to mix pages written in different languages inside one application (migration / rewrite / integration with very little time).
            so we created a trans_path function in each of the languages, and started simply doing trans_path("lib1"); so that the PHP version adds .php to the end of the lib, and ASP version .asp, etc.

            Then we even took it another step further, and made the function check whether the lib is already included:
            now we simply do include trans_path("lib1"); without the IF checking.
            the function does the checking
            function trans_path($lib)
            {
            $libIncludedVarName = $lib . "_included";
            if($$libIncludedVarName)
            $libPath = "emptyfile.php"
            else
            $libPath = $realPath . $lib . ".php"; // real path calculated right before this line
            return $libPath;
            }

            in above, emptyfile.php is in fact simply an empty file, and can therefore be included any number of times without generating the "already declared" error.

              I would also add into the discussion a suggestion from another board

              $root = $_SERVER['DOCUMENT_ROOT'];
              include $root . '/folder/foo.php';
              or
              opendir($root.'/folder/foo.php');

              Would this be in your list of unfortunates?

                Yes. Don't recall details, but it crapped out left and right on Windows, and on Linux it worked ok most of the time, but if I remember correctly, used to do something weird when we had different roots for ll hosts, for example
                www.domain.tld and
                secure.domain.tld
                again, not 100%, but i think it woud always give us the www.domain.tld root even from under secure.domain.tld, and (again not 100%) that was because we had domain.tld aliased to www
                that is, if we had set the server option not to reroute just http://domain.tld to [url]http://www.domain.tld,[/url] both would work fine, but once repointing (aliasing to be precise) was on, pages in all hosts under domain thought their root was the one that actually only www. had. Hope this was not too confusing.

                Also, if we set different roots for http://www.domain.tld and [url]httpS://www.domain.tld,[/url] all hell broke loose.

                I know at least one of our problems was due to a server bug that has been fixed since, but our method has worked for 2+ years without a single code change (code of this method), while everything else was changed a lot -- sites migrated from L to Win and back, moved between machines, merged, split, branched, all is still working. 🙂

                  azkworld

                  Thank you very much for you time here!

                  The option using

                  $_SERVER['DOCUMENT_ROOT'];

                  seems to meet our needs at present, but I will definately keep your option stashed in my back pocket for later if we find our selves in some of the complex situations you speak of.

                  Thanks again for all your time!

                  GRACIAS!!!

                    Write a Reply...