drudru
drudru

Reputation: 5023

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 in 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.

Upvotes: 0

Views: 1793

Answers (3)

Florian Holzhauer
Florian Holzhauer

Reputation: 703

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.

Upvotes: 2

hakre
hakre

Reputation: 198101

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?).

Upvotes: 1

daiscog
daiscog

Reputation: 12067

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.

Upvotes: 2

Related Questions