Declaring global variable with php.ini

2020-01-29 14:40发布

问题:

Is it possible to keep variables in php.ini file. Like that we do with the web.config in .net. I like to keep a flag type variable in the php.ini and use it to different projects.

回答1:

It's not possible to set user-level variables within a plain php.ini file (or the .htaccess equivilents). There are some PECL modules that do allow that, such as hidef (http://pecl.php.net/package/hidef) - though these would need to be installed on every installation you use.

Including (or pre-including) a file with auto_prepend_file is quite possible - though that would be on every PHP request.

What is frequently done is setting an environment variable as part of the webserver process, which can be read from PHP. In Apache this is quite easy, with the SetEnv module.

SetEnv PRODUCTION_SERVER 1

And accessing it in PHP:

if ($_ENV['PRODUCTION_SERVER']) {...}  // or getenv('PRODUCTION_SERVER')


回答2:

Have you looked at get_cfg_var()?

I needed to do something similar, and this was able to do it for me.



回答3:

Nope.

You could use the auto_prepend_file directive to automatically include a file that said, although as it uses the include_path, you'd need to specify the full path.

However, it's probably more transparent just to explicitly include/require the relevant file.



回答4:

One technique that I have found useful for passing a limited number of global variables to a bootstrap script is to take advantage of the SetEnv directive in an .htaccess file. The advantage is that the variable you set will be made available to any script in that directory, plus any scripts in child directories under it.

You could use a SetEnv varibale with the location of a configuration file, such as:

in .htaccess:

SetEnv init_path /home/hendepher/TestApp/init/init.php

In your .php scipt:

<?php
    if(!getenv('init_path')) throw new Exception('Must set init_path in .htaccess');
    require_once getenv('init_path');
    .
    .
    .

?>

If you have a test directory that requires different initialization o global variables, simply add another .htaccess file in your test directory:

SetEnv init_path /home/hendepher/TestApp/init/testing_init.php

Doing it this way, as opposed to using the 'auto_prepend_file' directive, is that your global configuration script is not run by all the php applications on your server: some may not need it.



回答5:

The accepted answere also worked for me, with one change.

I didn't test this on earlier versions, but in my environment (php 5.4.22) this doesn't show up in $_ENV, but rather in $_SERVER.

In my .htacess file:

SetEnv PRODUCTION_SERVER 0.  

My php code:

$production="PRODUCTION";
if (!isset($_SERVER['PRODUCTION_SERVER']) || $_SERVER['PRODUCTION_SERVER'] != 1){
    $production="DEVELOPMENT";
}


回答6:

I don't think that's a good place to store variables. php.ini is for storing configuration for PHP itself not your applications. You should consider putting the shared variables into a .inc file and including that instead.



回答7:

Have you considered hidef?

Allow definition of user defined constants in simple ini files, which are then processed like internal constants, without any of the usual performance penalties.