Asked  8 Months ago    Answers:  5   Viewed   47 times

I am trying to set an environment variable in an .htaccess file and retrieve it in PHP. I've looked through a bunch of other threads here on SO but everything I've tried so far has failed.

I've added this line to the .htaccess file:

SetEnv SPECIAL_PATH /foo/bin

I have tried retrieving this value using the getenv() PHP function:

<?php $specialPath = getenv('SPECIAL_PATH'); ?>

I have runned phpinfo() to see the list of available environment variables, SPECIAL_PATH is not there. I am puzzled as to why this is not working.

Thank you!

 Answers

36

Assuming your configuration has AllowOverrides with .htaccess, you must enable mod_env in Apache for this to work.

Apache - mod_env

Wednesday, March 31, 2021
 
Kemrop
answered 8 Months ago
92

Take a look at this:

<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

You have enabled AllowOverride All on the root directory, but then disabled it on /var/www/. I am going to guess that you intended to use Location / instead of Directory /.

Additional clarification AllowOverride has to be in an Directory section. Location can override directives in Directory if supported in Location.

Saturday, May 29, 2021
 
EastSw
answered 5 Months ago
34

Update

As of 17th May 2017 the README of autoenv states that direnv is probably the better option and implies autoenv is no longer maintained.

Old answer

I wrote autoenv to do exactly this:

https://github.com/kennethreitz/autoenv

Wednesday, June 9, 2021
 
leebriggs
answered 5 Months ago
48

You are slightly wrong. The export makes the setting available for all commands and subprocesses in the current shell. Setting it on the command line sets it only for the duration of that command.

If you are writing a shell script, it's quite normal to set the variable and export it once, so you don't have to do it on each line. The value will only be in effect during the execution of the shell script1. It won't affect the parent process that calls the shell script, only the commands within the shell script. You won't need to unset the value at the end of the shell script.

1 assuming you aren't running the shell script with . or source.

Thursday, August 26, 2021
 
Cheers and hth. - Alf
answered 2 Months ago
98

Without using expression i.e. if/else directives you can do this:

# set EARLY_VAR to 1
SetEnvIf Host ^ EARLY_VAR=1

# if EARLY_VAR is 1 then set TEST_VAR to if_branch
SetEnvIf EARLY_VAR ^1$ TEST_VAR=if_branch

# if EARLY_VAR is NOT 1 then set TEST_VAR to else_branch
SetEnvIf EARLY_VAR ^(?!1$) TEST_VAR=else_branch

This will work with even older Apache versions i.e. <2.4


EDIT:

In your Apache config or vhost config have this variable initialization:

SetEnvIf Host ^ EARLY_VAR=5

Then in your .htaccess you can use:

<If "env('EARLY_VAR') == '5'">
   SetEnv TEST_VAR if_branch
</If>
<Else>
   SetEnv TEST_VAR else_branch
</Else>

Reason why you need env variable declaration in Apache/vhost config because .htaccess is loaded after Apache/vhost config and env variables set in same .htaccess are not available for evaluation in if/else expressions.

Also, note that it is important to use SetEnvIf instead of SetEnv to make these variables available to if/else expressions.

Sunday, September 19, 2021
 
1.21 gigawatts
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share