Overriding $DOCUMENT_ROOT in an Apache Module

php.internals

Mark Morley

22 years ago
I'm working on a custom mass hoting Apache module. I need to be able to set the $DOCUMENT_ROOT value on a per request basis. That is, I'd like to be able to set it in my module before the clients' scripts run (similar to how I use zend_alter_ini_entry to alter ini values). I've tried using Apache's ap_table_set call to define the environment variable, but PHP seems to clobber that and replace it. Any thoughts? Mark

Rasmus Lerdorf

22 years ago
On Wed, 24 Sep 2003, Mark Morley wrote:
> I'm working on a custom mass hoting Apache module. > > I need to be able to set the $DOCUMENT_ROOT value on a per > request basis. That is, I'd like to be able to set it in my module before > the clients' scripts run (similar to how I use zend_alter_ini_entry to > alter ini values). > > I've tried using Apache's ap_table_set call to define the environment > variable, but PHP seems to clobber that and replace it. > > Any thoughts?
This doesn't really have anything to do with PHP. You are writing an Apache module. You simply have to change the doc_root before Apache gets to the content handling phase of the request_rec. And to change the doc_root I think you'd just fiddle with conf->ap_document_root and it should all fall into place. But again, this has nothing to do with PHP. -Rasmus

Mark Morley

22 years ago
> This doesn't really have anything to do with PHP. You are writing an > Apache module. You simply have to change the doc_root before Apache gets > to the content handling phase of the request_rec. And to change the > doc_root I think you'd just fiddle with conf->ap_document_root and it > should all fall into place. But again, this has nothing to do with PHP. > > -Rasmus
Thanks for the reply, but that's not it. I'm already setting the document root as far as Apache is concerned, at it works fine for everything *except* PHP. That is, PHP scripts are loaded and run properly, except that $DOCUMENT_ROOT does not reflect the value I set. So the question remains, how can I override PHP's opinion of what the document root is (if at all)? Mark

Rasmus Lerdorf

22 years ago
On Wed, 24 Sep 2003, Mark Morley wrote:
> > This doesn't really have anything to do with PHP. You are writing an > > Apache module. You simply have to change the doc_root before Apache gets > > to the content handling phase of the request_rec. And to change the > > doc_root I think you'd just fiddle with conf->ap_document_root and it > > should all fall into place. But again, this has nothing to do with PHP. > > > > -Rasmus > > Thanks for the reply, but that's not it. I'm already setting the > document root as far as Apache is concerned, at it works fine for > everything *except* PHP. That is, PHP scripts are loaded and run > properly, except that $DOCUMENT_ROOT does not reflect the value > I set. So the question remains, how can I override PHP's opinion > of what the document root is (if at all)?
Actually, that is it. PHP does not have its own version of that value. It simply loops through Apache's subprocess_env and sets whatever is there in the $_SERVER array. So if you set it correctly in Apache it will be set correctly in PHP. If it isn't set in PHP, you didn't set it correctly in Apache. -Rasmus

Chris Murley

22 years ago
Then your not setting it correctly via your apache module. FYI - If you using apache 1.3.x you CANNOT reset document root. DOCUMENT_ROOT is set at a low level of initial child request startup. Your third party module isnt at a low enough level to do this. I kow this because my company needed the same thing. Our developers ended up maintaining a patch to apache 1.3.x source tree. We do it at the httpd.c level, and NOT the module level. This isn't PHP's fault, it's apaches. On Wed, 24 Sep 2003, Mark Morley wrote:
> > This doesn't really have anything to do with PHP. You are writing an > > Apache module. You simply have to change the doc_root before Apache gets > > to the content handling phase of the request_rec. And to change the > > doc_root I think you'd just fiddle with conf->ap_document_root and it > > should all fall into place. But again, this has nothing to do with PHP. > > > > -Rasmus > > Thanks for the reply, but that's not it. I'm already setting the > document root as far as Apache is concerned, at it works fine for > everything *except* PHP. That is, PHP scripts are loaded and run > properly, except that $DOCUMENT_ROOT does not reflect the value > I set. So the question remains, how can I override PHP's opinion > of what the document root is (if at all)? > > Mark > >
-- Regards, -Chris __________________________________________________________________ Christopher Murley PHONE: 309.743.0800 Senior Systems Administrator TOLL FREE: 800.293.9576 TownNews.com \ Lee Enterprises FAX: 309.743.0830 1521 47th Avenue E-Mail: murley@TownNews.com Moline, Illinois 61265 Web: http://www.TownNews.com ------------------------------------------------------------------

Mark Morley

22 years ago
> Then your not setting it correctly via your apache module.
Actually I am setting it correctly, but as you say...
> FYI - If you using apache 1.3.x you CANNOT reset document > root. DOCUMENT_ROOT is set at a low level of initial child request > startup. Your third party module isnt at a low enough level to do this.
..Apache replaces certain variables later on in the processing chain, including DOCUMENT_ROOT. I found a way to avoid this though, and now my module is working the way I want and I am able to specify the DOCUMENT_ROOT dynamically. Thanks for the replies though, they helped track it down. Mark

Jeremy S. Johnstone

22 years ago
Care sharing what you did to fix it? On or offlist is fine. Jeremy On Thu, 2003-09-25 at 10:22, Mark Morley wrote:

Mark Morley

22 years ago
> Care sharing what you did to fix it? On or offlist is fine. > > Jeremy
Probably the same thing that the other fellow wound up doing. In src/main/util_script.c Apache explicitly adds a new DOCUMENT_ROOT value (among others) to the environment table. This effectively wipes out any DOCUMENT_ROOT value that I'd set in my module. Any value that Apache does NOT override later on gets passed through to PHP's environment just fine. I changed util_script.c to check for a DOCUMENT_ROOT value first so it only adds one if it doesn't already exist. It means maintaining a patch for Apache, but I already do that for that file anyway due to some other unrelated cusomizations. If there's a better way I'm all ears, but this works. Mark