Declaring global variable with php.ini

后端 未结 8 1833
死守一世寂寞
死守一世寂寞 2020-12-03 21:12

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 pro

相关标签:
8条回答
  • 2020-12-03 21:47

    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";
    }
    
    0 讨论(0)
  • 2020-12-03 21:49

    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.

    0 讨论(0)
  • 2020-12-03 21:57

    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.

    0 讨论(0)
  • 2020-12-03 22:02

    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.

    0 讨论(0)
  • 2020-12-03 22:04

    Complementing @Ascherer answer, use get_cfg_var() to save custom variables in custom php.ini (variable created by you, not an official PHP ini directive). For example:

    In php.ini: custom_variable = "abcde"
    In any php script: get_cfg_var('custom_variable') returns abcde

    I use this in in a small project in local dev. As I run the local server via php -S localhost:8000 -c php.ini (not running an Apache server locally), it's a good option to call some configuration constants. In production, these constants are set in .htaccess.

    0 讨论(0)
  • 2020-12-03 22:07

    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')
    
    0 讨论(0)
提交回复
热议问题