Use Environment Variables with If Statements

2020-07-08 06:31发布

I was wondering if there's a way to use an Environment Variables (or similar) with an if statement in order to turn certain things on and off, such as display_errors, modpagespeed.

So for example:

SetEnv DISPLAY_ERRORS on

<If "DISPLAY_ERRORS == on">
    php_flag display_errors on
</If>
<If "DISPLAY_ERRORS == off">
    php_flag display_errors off
</If>

I've spent quite a while searching for such a thing but I was unable to uncover anything which does this. Is this even possible?

I'm looking for an easy way to edit the htaccess using PHP, but instead of writing blocks to htaccess it would be easier to just change the "on" and "off" values and would also make it easier to display this as radio buttons within my application.

1条回答
Juvenile、少年°
2楼-- · 2020-07-08 06:37

We can be tempted to write this:

#SetEnvIf Request_URI "^" DISPLAY_ERRORS=off
SetEnv DISPLAY_ERRORS off

<If "-T env('DISPLAY_ERRORS')">
    php_flag display_errors off
</If>
<Else>
    php_flag display_errors on
</Else>

But it seems that <If> is evalued before any SetEnv(If).

Note that the value of a variable may depend on the phase of the request processing in which it is evaluated. For example, an expression used in an <If > directive is evaluated before authentication is done

A different working approach I have in mind would be:

<IfDefine DISPLAY_ERRORS>
    php_flag display_errors on
</IfDefine>
<IfDefine !DISPLAY_ERRORS>
    php_flag display_errors off
</IfDefine>

And starting Apache with a -DDISPLAY_ERRORS argument to display errors or, for Apache >= 2.4, add a Define DISPLAY_ERRORS in the virtualhost configuration.

查看更多
登录 后发表回答