Hello all,
I cooked up a patch that gives PHP support for per-directory
php.ini files that properly inherit. This makes php.ini files
act a little like .htaccess files. We are cognizant of the associated
performance issues, and thus this is a strictly optional feature.
For a little history, the current, common approach
is to set PHPRC to be the current directory, so that a php.ini
file from that directory is read. This unfortunately also overloads
the global php.ini file, has an odd ordering with respect to php.d,
and means that you have to place a php.ini file/symlink in every
directory where PHP scripts could be run.
While the patch is no where near where I would be comfortable
formally submitting it for review on internals, I'd like to gauge
how friendly the core PHP developers would be to an enhancement like
this. In a nutshell, here is how the patch works:
In php_ini.c, php_init_config() is patched to call php_user_init_config()
if user directory php.ini's are accepted. The function then:
- Retrieves the current working directory
- Checks if the cwd is inside the user's home directory
- Successively walks up from the user's home directory to the
cwd, checking if a php.ini exists and loading it with
zend_parse_ini_file if it does.
We also have two special cases of behavior that we found helpful for our
use-case:
-
We serve only a user's ~/web_scripts directory, so perform an optional
check to make sure the PHP file is in the right place, and to not
attempt to load ~/php.ini -
If we had Apache frob $HOME such that it points to a "different" home
directory, which is the web serve directory, skip the previous check.
To get an idea for its behaviors, take the following example:
/etc/php.ini
foo = 1
bar = 1
/mit/user/web_scripts/wiki/php.ini
foo = 2
Then, we see the following behavior for the following PHP scripts
/mit/user/web_scripts/test.php
foo = 1
bar = 1
/mit/user/web_scripts/wiki/test.php
foo = 2
bar = 1
/mit/user/web_scripts/wiki/subdir/test.php
foo = 2
bar = 1
I am looking forward to your comments, and I can post the very rough
(but functional) patch if you are so interested.
Cheers,
Edward
hi,
Hello all,
I cooked up a patch that gives PHP support for per-directory
php.ini files that properly inherit. This makes php.ini files
act a little like .htaccess files. We are cognizant of the associated
performance issues, and thus this is a strictly optional feature.
The patch is missing. However I already have a question. I hope you
know that we introduced the ini per dir in 5.3. It supports user ini
files in the document root, [PATH=...] in the main php.ini, etc. Did
you improve it or started from scratch?
Btw, for previous php versions, did you look at pecl's htscanner as well?
Cheers,
Pierre
Excerpts from Pierre Joye's message of Mon Aug 24 18:03:14 -0400 2009:
The patch is missing.
I intentionally omitted it, since it had some code specific to our
environment. I have posted it here:
http://web.mit.edu/~ezyang/Public/php-scripts.patch
It is very very rough and does things that would never ever be allowed
in the PHP core.
However I already have a question. I hope you
know that we introduced the ini per dir in 5.3. It supports user ini
files in the document root, [PATH=...] in the main php.ini, etc. Did
you improve it or started from scratch?
The patch is off of PHP 5.2 (which we have deployed); I wasn't aware until
recently of the new ini per dir functionality. I don't think it has
quite the same functionality though.
Btw, for previous php versions, did you look at pecl's htscanner as well?
I had not heard of htscanner, and on the prompting of your mail went and
looked it up. That is a very interesting implementation, although due
to backwards-compatibility we can't exactly tell all of our users to convert
their php.ini files into htaccess files ;-)
If I have misinterpreted, please let me know (I only made cursory glances
through the associated diffs/source code).
Cheers,
Edward
hi,
Excerpts from Pierre Joye's message of Mon Aug 24 18:03:14 -0400 2009:
The patch is missing.
I intentionally omitted it, since it had some code specific to our
environment. I have posted it here:http://web.mit.edu/~ezyang/Public/php-scripts.patch
It is very very rough and does things that would never ever be allowed
in the PHP core.However I already have a question. I hope you
know that we introduced the ini per dir in 5.3. It supports user ini
files in the document root, [PATH=...] in the main php.ini, etc. Did
you improve it or started from scratch?The patch is off of PHP 5.2 (which we have deployed); I wasn't aware until
recently of the new ini per dir functionality. I don't think it has
quite the same functionality though.
Please point out the differences and list what else you may need. I do
not think we will introduce another feature but improve the current
implementation.
Btw, for previous php versions, did you look at pecl's htscanner as well?
I had not heard of htscanner, and on the prompting of your mail went and
looked it up. That is a very interesting implementation, although due
to backwards-compatibility we can't exactly tell all of our users to convert
their php.ini files into htaccess files ;-)If I have misinterpreted, please let me know (I only made cursory glances
through the associated diffs/source code).
It is easy to make htscanner support ini format instead :)
Cheers,
Pierre
Excerpts from Pierre Joye's message of Mon Aug 24 18:21:32 -0400 2009:
Please point out the differences and list what else you may need. I do
not think we will introduce another feature but improve the current
implementation.
As I understand it, the features are not compatible. One takes an
explicit list of directories to scan, while the other dynamically
determines it based on the current working directory and the
user's home directory. (There are some other features on top
of this, but that's the essential functionality). Please let me
know if I am mistaken.
It is easy to make htscanner support ini format instead :)
That is true. However, it would be nice to see this go into the
core, so as to encourage wide use (also, htscanner is something
of a hack, quite frankly.)
Cheers,
Edward
Excerpts from Pierre Joye's message of Mon Aug 24 18:21:32 -0400 2009:
Please point out the differences and list what else you may need. I do
not think we will introduce another feature but improve the current
implementation.As I understand it, the features are not compatible. One takes an
explicit list of directories to scan, while the other dynamically
determines it based on the current working directory and the
user's home directory. (There are some other features on top
of this, but that's the essential functionality). Please let me
know if I am mistaken.
There is two modes:
- It looks in the current directory for ini file (named by default
.user.ini) and scan the parent directories up to the document root to
apply rules from other .user.ini. - new directives in php.ini using sections, like
[PATH=/home/www/site1] or [HOST=...]
It is easy to make htscanner support ini format instead :)
That is true. However, it would be nice to see this go into the
core, so as to encourage wide use (also, htscanner is something
of a hack, quite frankly.)
It is in the core now, and from an implementation point of view there
are very few differences.
Cheers,
Pierre
Excerpts from Pierre Joye's message of Mon Aug 24 19:37:38 -0400 2009:
- It looks in the current directory for ini file (named by default
.user.ini) and scan the parent directories up to the document root to
apply rules from other .user.ini.
My apologies, I was reading php_ini.c and didn't realize the appropriate
logic was in a different file. Now having read php_cgi_ini_activate_user_config()
I believe that the new functionality perfectly fits our use case.
Thanks,
Edward