I'm trying to set up a document root definition for my program to make it more portable. As it stands, I'm doing things like this:

# /myApp/index.php

# ini_set("doc_root", "http://localhost/myApp/"); // Doesn't work, import fails
$_SERVER['DOCUMENT_ROOT'] = "includes/myinclude.php"; // gets rid of the error

require_once($_SERVER['DOCUMENT_ROOT'] . "includes/myinclude.php");

I don't get any errors on the include when I forcibly set the value with the = sign, but the file doesn't seem to be imported correctly. When I try to access the class inside, it says the class doesn't exist.

However, if I copy myinclude.php directly into the same folder as index.php, then use require_once("myinclude.php") everything works perfectly.

Any thoughts?

    $_SERVER['DOCUMENT_ROOT'] (as per documentation)
    "The document root directory under which the current script is executing, as defined in the server's configuration file."

    Which means it will return you (in my case E:\HTDOCS) or in your case C:\APACHE\HTDOCS or whatever.

    In cases like yours, use I'd do something like this...

    define ('HOST', 'http://localhost/');
    define ('APP_PATH', 'home/');
    define ('INCLUDE_PATH', 'includes/');

    and then use require (HOST.INCLUDE_PATH."$filename"); to get the file.

      Oh, ok. So, basically what I want to do is define the include path. Can I use set_include_path for that?

      And DOCUMENT_ROOT is more suitable for my HTML, like image tags, stylesheets, etc. that are included like <img src='/images/logo.png' />

      Is that correct?

        Preferably I won't use DOCUMENT_ROOT for anything other than viewing pages for myself on my own server, which i can do anyway using explorer.

        The include_path directive in PHP.ini is meant for the whole server. So if you are setting up just one site, you CAN use that. I'd recommend not to go that way.

        Even for your HTML, images etc. use the HOST.APP_PATH combo. The reason is that with DOCUMENT_ROOT you are accessing files directly from BROWSER/EXPLORER and there is no webserver involved.

          The big problem was that by including my file via http:// I was getting the parsed version. So I wasn't getting an error, but I couldn't access the classes inside. I had to include it from my filesystem protocol instead.

            Write a Reply...