Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From PHP, test if Apache variable is defined (same as IfDefine in .htaccess)

How to test from PHP whether an Apache variable is defined? In .htaccess this is tested with IfDefine.

When starting Apache, I define a variable: in Windows with

httpd.exe -D "MYVARIABLE" -w -n "Apache2.2" -k start

and in Ubuntu adding to /etc/apache2/envvars:

export APACHE_ARGUMENTS='-D MYVARIABLE'

Now I can use it in .htaccess as

<IfDefine MYVARIABLE>
    do stuff
</IfDefine>

and it works.

How can I test for it from PHP? I tried

if (getenv ('MYVARIABLE')) do stuff;
if (apache_getenv ('MYVARIABLE')) do stuff;

but it always returns FALSE.

phpinfo ();

prints a lot of things but there is no string MYVARIABLE in its output.

like image 429
Alexander Gelbukh Avatar asked Nov 18 '25 14:11

Alexander Gelbukh


1 Answers

Thanks to mario's comment, I think the answer is:

  • On Windows, probably no way when the variable is set as a command line argument, such as

    httpd.exe -D "MYVARIABLE" -w -n "Apache2.2" -k start
    
  • On Linux/Ubuntu, if it is set in /etc/apache2/envvars as

    export APACHE_ARGUMENTS='-D MYVARIABLE'
    

    then from PHP

    $x = getenv ('APACHE_ARGUMENTS')
    

    gives -D MYVARIABLE, from which you can use strstr ($x, 'MYVARIABLE') or so to find the variable.

like image 108
Alexander Gelbukh Avatar answered Nov 21 '25 03:11

Alexander Gelbukh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!