Dynamic path to auto_prepend_file for PHP?

Hi,

I’d like to have an auto_prepend_file executing based on a relative path. For example, I’d like to only set it once in my top level .htaccess file like:

php_value auto_prepend_file “./auto_prepend_file.php”

The problem is that when I execute a page in a different directory, Apache tries to load the file as it were in the same directory as the requested file, even though the .htaccess file is NOT. Is there a way to do this? I thought about using DOCUMENT_ROOT but nothing, it’s just seen as “DOCUMENT_ROOT/auto_prepend_file.php”

-m

mitch,

PHP is behaving in the correct way - as you directed! “./” is the 'nix way to say THIS directory whereas “/” says DocumentRoot. In other words, delete the leading dot and you should be fine.

Regards,

DK

Hey sorry for not getting back to this.

I’m trying to find a way to set an include path for an entire directory and sub-directories, but using a relative path. For example, put a .htaccess file in the root of the site, set the include_path to something like “./includes/” and then have every page make use of that setting

But that doesn’t work. Apache is looking at the “./” as relative to the requested file, NOT the .htaccess file. I’d love to be able to change that to:

php_value include_path ${DOCUMENT_ROOT}/includes/

That obviously doesn’t work… Is something like this possible?

  • matt

matt,

Isn’t “/includes” what you want? That IS the DocumentRoot of your site as far as Apache is concerned.

Okay, for others:

/includes says to use the includes (directory) in the DocumentRoot (base level of the website).

./includes says to use the includes (directory) in the current directory.

…/includes says to use the includes (directory) in the parent (one up) directory.

Regards,

DK

Actually whenever I put a / first, Apache is using the system path (absolute). I’ve not been able to get it to use it as a path relative to DOCUMENT_ROOT. What do you think is up with that? Are you getting a path relative to D.R. using a / first? Thanks for your help.

  • matt

OK my solution (stolen from some site found by Google) is to add multiple relative include paths. Like this:

php_value include_path ./private/includes/:…/private/includes/:…/…/private/includes:…/…/…/private/includes/:…/…/…/…/private/includes/

It isn’t very nice, but it works. Anyone see a problem with doing something like this?

  • matt

EDIT: I can’t imagine ever going deeper than 4-5 levels on a site.

matt,

Yes, that’s ridiculous! :nono:

Just use the /private/includes and be done with it.

Regards,

DK

You see that’s the problem though. PHP uses an absolute path to the system not the web servers document_root. So you set the include_path in .htaccess and it’s either absolute (/etc/http/ etc.) OR relative to the location of the executing script. So, if it’s relative, it’s always changing depending on the depth of your executing script.

If you have a .htaccess file at the root of the site and the include_path is set to ./private/includes/, and the executing script is mysite.com/about/index.php, the include_path will then become ./about/private/includes/ - which isn’t correct.

/private/includes is a directory in my website. It’s located at the document_root. So /Library/WebServer/Documents/private/includes/

Does this make sense?

matt,

Argh! Sorry, mate! I forgot that you were dealing with the path (in your physical system rather than in the site). Doing that, all you need is the single definition of where the includes is located so /Library/WebWerver/Documents/private/includes will do the trick for you.

Regards,

DK

OK yes you are right. Make an absolute path but… the problem now is that we have a dev server and the different server setups for the live servers for each site. The path is always changing depending on the live host… and I’d love to find a way to set the include path so that it ALWAYS works. The crazy solution I posted above will do that. But it seems a little nutty. What I’d love to see is something like:

php_value include_path %{DOCUMENT_ROOT}/private/includes/

But no way that’s going to work. I actually think the real solution is to start using a front controller and forget about this?

  • matt

You’re nearly there:


php_value include_path ".:/usr/share/php:/full/system/path/to/private/includes"

The first two entries are the default include_path. Only works in Unix btw. Windows uses ; to separate the entries. You can’t use Apache env vars there.

matt,

Permit a slight translation: You can specify a physical path for PHP scripts that are OUTSIDE the webspace, i.e., NOT under Document_Root. If all your sites are under a single directory (above DocumentRoot), then you can point there for all your domains. However, as LinhGB pointed out, the path differences between WinDoze and *Nix mean that you’ll have to include two paths to catch both (on the WILD :injured: assumption that neither OS will choke on the other’s path info).

Regards,

DK

Nobody uses Windows to run Apache and PHP in a production environment anyway so you can safely ignore that difference. :wink:

AMEN!

Okay, change that to “Nobody in the right mind uses …” :smiley:

Regards,

DK