开发者

is there a way to preload code in php in the Apache parent?

I want to run some code in Apache at initialization before PHP requests are handled in the Apache child processes. This would have to happen i开发者_如何转开发n the Apache parent. It would setup some new global variables.

When the child runs, there would be these new variables there ready and waiting.

APC (and the like) are not an option since this code is kind of expensive to run.

I'm trying to avoid writing a PHP module.


Personally, I place php_value auto_prepend_file config.inc.php in .htaccess (or in httpd.conf) to get config.inc.php to run before any PHP scripts are executed to set up my global vars.

Note that this is run on every request, though, so if it's a large setup script, it will be expensive.

Also, this will not work if PHP is installed as CGI, in which case you'll need to set the auto_prepend_file setting in php.ini.


The best way to go would be as told by daiscog to use auto-prepend. If you need to set it within apache for some reason, you could pass them using environment variables:

Set them with apache as you like, https://httpd.apache.org/docs/2.2/env.html, and access them in PHP using $_ENV - while this wont obviously not work for "complex" php variables like arrays, you could use it to pass strings from apache to php.


Apache itself can not execute pre-loaded PHP code and set variables per parent. It's a compiled application and it only executes PHP code via the server api, it does not share much with PHP memory wise. That are basically two worlds.

However what works is a webserver that is build in PHP. Such a webserver can hold variables between HTTP requests. Infact all global variables. So your code must be compatbile with that pattern and cleanly shutdown the part that should get unloaded (the code will never get unloaded, only the global variables that get unset).

Such a webserver is appserver-in-php.

However I do not exactly understand how/why/for-what you want to prevent writing a PHP module (and which kind? compiled? In userspace?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜