Declaring global variable with php.ini

后端 未结 8 1884
死守一世寂寞
死守一世寂寞 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: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:

    
    

    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.

提交回复
热议问题