Last Updated: February 25, 2016
·
397
· gurix

Set values over environment variables in PHP Projects

Imagine you don't want store some sensitiv informations like passwords in your code and you want still keep a part of your configuration in your version control repository.

One approach I describe here is simply using $_ENV variables.

i.e: $_ENV["APPLICATION_DB_PASSWORD"] in someproject/application/config/development.rb for yii framework

return array(
    'name' => 'LimeSurvey',
    'components' => array(
        'db' => array(
            'connectionString' => 'mysql:host=localhost;port=3306;dbname=myawsomedb',
            'emulatePrepare' => true,
            'username' => 'someuser',
            'password' => $_ENV["APPLICATION_DB_PASSWORD"],
            'charset' => 'utf8',
            'tablePrefix' => 'xyz_',
        ))

With .htaccess...

SetEnv APPLICATION_DB_PASSWORD "shpxlbh"

If you are using PHP builtin webserver you just have to set the environment variable in your terminal or add it permanently to your ~/.bashrc

export APPLICATION_DB_PASSWORD="shpxlbh"