How to get system environment variables into PHP while running CLI & Apache2Handler?

前端 未结 4 511
梦如初夏
梦如初夏 2020-11-28 05:13

My system is Ubuntu and I have set my environment variables in /etc/environment.

If I\'m running PHP script using

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 05:54

    On ubuntu, PHP uses different ini files for regular and CLI processes.

    There should be few ini files like /etc/php5/cli/php.ini, /etc/php5/fpm/php.ini or /etc/php5/php.ini. Open related INI file and change the

    variables_order = "GPCS"

    line to

    variables_order = "EGPCS".

    After that, you would get the environment variables which you set before using $_ENV['varname'].

    From php.ini about variables_order :

    Abbreviations for the following respective super globals: GET, POST, COOKIE,
    ENV and SERVER. There is a performance penalty paid for the registration of
    these arrays and because ENV is not as commonly used as the others, ENV is
    is not recommended on productions servers. You can still get access to
    the environment variables through getenv() should you need to.
    

    So you can try to use getenv() instead of $_ENV[].

提交回复
热议问题