Setting PHP variables in httpd.conf?

后端 未结 6 1504
不思量自难忘°
不思量自难忘° 2020-12-24 14:55

I\'d like to automatically change my database connection settings on a per-vhost basis, so that I don\'t have to edit any PHP code as it moves from staging to live and yet a

相关标签:
6条回答
  • 2020-12-24 15:08

    Yep...you can do this:

    SetEnv DATABASE_NAME testing
    

    and then in PHP:

    $database = $_SERVER["DATABASE_NAME"];
    

    or

    $database = getenv("DATABASE_NAME");
    
    0 讨论(0)
  • 2020-12-24 15:13

    The problem with .htaccess is that it is part of the code base tree. And the code base tree is part of VC/SVN. Hence any change in local/dev gets moved to production. Keeping the env variable setting in httpd.conf saves you the effort of being careful about not accidentally overwriting the server vs dev flag. Unless of course you want to do with IP address or host name, both of which are not scalable approaches.

    0 讨论(0)
  • 2020-12-24 15:17

    You can set an environment variable and retrieve it with PHP.

    In httpd.conf:

    SetEnv database testing
    

    In your PHP:

    if (getenv('database') == 'testing') {
    

    or

    if ($_SERVER['database'] == 'testing') {
    
    0 讨论(0)
  • 2020-12-24 15:17

    I would not set an environment variable, as this is also visible in default script outputs like PhpInfo();

    just use a php_value in your .htaccess just above the htdocs folder and you're done and safe :)

    0 讨论(0)
  • 2020-12-24 15:20

    Did you tried to use the .htaccess file? You could override the php.ini values using it.

    Just put the .htaccess file into your htdocs directory:

    php_value name value
    

    Futher information:

    • https://php.net/manual/en/configuration.changes.php
    • https://php.net/manual/en/ini.php
    0 讨论(0)
  • 2020-12-24 15:24

    I was also looking at this type of solution. What I found is this, under Apache you can use the SetEnv KeyName DataValue in the http.conf and in IIS you can use Fast CGI Settings >> Edit... >> Environment Variables >> ... and add KeyName, DataValue.

    This in turn allows the PHP $var = $_SERVER["KeyName"]; to be set to the DataValue and used as needed under both IIS and Apache consistently.

    I know this is a strange use case. I use WAMP at work and MAMP at home so it is nice to be able to work the same way.

    0 讨论(0)
提交回复
热议问题