简体   繁体   中英

Set and check an environment variable

I am trying to set an environment variable depending on if PHP is running as an apache module from within an .htaccess file. Dumping out the $_SERVER array in the php script I can see that the MOD_PHP variable is successfully set, but the reqenv() condition always seems to return false, since ini_get('memory_limit'); still reports the default value.

<IfModule mod_php5.c>
    SetEnv MOD_PHP php5
</IfModule>
<IfModule mod_php7.c>
    SetEnv MOD_PHP php7
</IfModule>
<IfVersion >= 2.4>
    <If "reqenv('MOD_PHP') != '' || reqenv('REDIRECT_MOD_PHP') != ''">
        php_value memory_limit 1024M
    </If>
</IfVersion>

Not sure why the above is not working. If I move the php_value right beneath the <IfVersion> section then the value is modified as expected:

<IfModule mod_php5.c>
    SetEnv MOD_PHP php5
</IfModule>
<IfModule mod_php7.c>
    SetEnv MOD_PHP php7
</IfModule>
<IfVersion >= 2.4>
# the following works
php_value memory_limit 1024M
</IfVersion>

So it seems to have something do to with the <If "reqenv()"> part.

I am running php 7.2.13 on ubuntu 14.04 with apache 2.4.7 installed.

I might also add that I am running PHP as an apache module.

SetEnv happens very late, before <if> is evaluated.

SetEnvIf happens much earlier, before <if> is evaluated.

Use SetEnvIf if you want to set an per-request environment variable early enough to check it with <if> . Or, use a real environment variable (defined in eg the envvars file distributed with httpd) if the variable is not per-request at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM