19

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.

Zstream
  • 205
  • 1
  • 2
  • 6
  • Assuming local access (considering your mention of the php.ini) just use a local file and reference it in your project (/usr/bin/local/myconfig.ini or C:\PHP\myconfig.ini) – Brad Christie Feb 19 '11 at 18:10

8 Answers8

26

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')
Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
  • Btw did a small benchmark: `ini_get` and `getenv` take the *same* time (after 10M loops) when both values are string of same length. There is a slight difference (more time) for the one which has a slightly longer string (etc...). – Déjà vu Oct 07 '12 at 11:42
  • This is unsuitable for me just adding, because I want sensitive passwords only readable by root, and my php application has both a cron aspect and web aspect thus, setting only apache env wont solve my cron for now. However this is useful on apache side. – beiller Dec 26 '13 at 17:50
  • The provided solution is not functional if you are also using CLI and CRONS, a configuration file would be the most flexible way to do it. – johnlemon Feb 19 '15 at 08:43
  • 1
    On my system (php 5.4.22) this doesn't show up in $_ENV, but rather in $_SERVER. My answer below. – Craig Jacobs Aug 17 '15 at 21:01
  • Take a look at @Ascherer answer. `get_cfg_var()` will return a value from php,ini – pumbo Apr 08 '16 at 10:29
  • PHP 7 also includes any ini files that are present in the dir `/etc/php/7.0/cli/conf.d` You can add you own ini file(s) there, without interfering with php.ini (and the php-update flow). – andkrup Jul 05 '17 at 14:00
  • I just tried that and the variable is NOT accessible with `$_ENV`. It shows as `$_SERVER` index in my case here (PHP 5.4, Apache). – silverdr Dec 13 '17 at 09:48
8

Have you looked at get_cfg_var()?

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

Ascherer
  • 8,223
  • 3
  • 42
  • 60
4

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.

John Parker
  • 54,048
  • 11
  • 129
  • 129
1

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";
}
Craig Jacobs
  • 940
  • 2
  • 16
  • 28
1

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.

hendepher
  • 143
  • 5
0

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.

drewish
  • 9,042
  • 9
  • 38
  • 51
  • $production="PRODUCTION"; if (!isset($_SERVER['PRODUCTION_SERVER']) || $_SERVER['PRODUCTION_SERVER'] != 1){ $production="DEVELOPMENT"; } These are system level app configurations-- it helps move details out of the app and into the provisioning step. It is so nice to work in one code base where all the code just works because it picks up the environment specific details from outside the app codebase. – BradChesney79 Dec 08 '16 at 21:28
  • ^^ looked so much better with whitespace and linebreaks... sad face – BradChesney79 Dec 08 '16 at 21:29
0

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.

chx
  • 11,270
  • 7
  • 55
  • 129
  • `php5-dev` is; after that you can install with the `pecl` command. This is off topic for this site. – chx Dec 05 '14 at 19:28
0

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.

ofri cofri
  • 886
  • 10
  • 13