3

I would like to get the value of a environment variable and assign it to another variable in Apache.

For example, get the value of $_SERVER['REMOTE_ADDR'] assign it to $_SERVER['USER-IP'] and override $_SERVER['REMOTE_ADDR'] to 127.0.0.1

I am stuck on getting the value of REMOTE_ADDR

<IfDefine !USER-IP> SetEnv USER-IP %{REMOTE_ADDR} SetEnv REMOTE_ADDR '127.0.0.1' </IfDefine> 
1
  • The IfDefine is wrong here. IfDefine tests server parameters set on the command line, not environment variables. See the docs. You want If instead. Commented Feb 6, 2018 at 10:09

1 Answer 1

4

Looking at the docs, the $_SERVER['REMOTE_ADDR'] var is not strictly an environment variable, but a cgi request meta-variable provided by the web server to the cgi context; http://www.faqs.org/rfcs/rfc3875.html

Meta-variables contain data about the request passed from the server to the script

 meta-variable-name = "AUTH_TYPE" | "CONTENT_LENGTH" | "CONTENT_TYPE" | "GATEWAY_INTERFACE" | "PATH_INFO" | "PATH_TRANSLATED" | "QUERY_STRING" | "REMOTE_ADDR" | "REMOTE_HOST" | "REMOTE_IDENT" | "REMOTE_USER" | "REQUEST_METHOD" | "SCRIPT_NAME" | "SERVER_NAME" | "SERVER_PORT" | "SERVER_PROTOCOL" | "SERVER_SOFTWARE" | scheme | 

The apache docs indicate that these variables cannot be overriden using the standard SetEnvstyle directives

Some Caveats

It is not possible to override or change the standard CGI variables using the environment manipulation directives.
https://httpd.apache.org/docs/2.4/env.html#setting

So I think it's unlikely you can set those values easily from apache conf

Setting environment variables

(from existing server variables)

<Directory /var/www/server111> Order allow,deny Allow from all # This syntax works, as you can see from the image below... RewriteEngine On RewriteRule .* - [E=USER-IP:%{REMOTE_ADDR}] # none of these syntax seem to work SetEnv USERIP %{REMOTE_ADDR} SetEnv USERIP2 blah SetEnv USERIP3 ${REMOTE_ADDR} SetEnv USERIP6 %{ENV:REMOTE_ADDR} </Directory> 

enter image description here

5
  • Thanks, the second part of the question is if I can get the value of the variable SetEnv USER-IP %{REMOTE_ADDR} Commented Feb 6, 2018 at 14:24
  • The only way I can see to do it is using a rewriteRule, which has access to those variables. The SetEnv didn't seem to be able to read %{ENV} vars at all... I've updated the answer with an example Commented Feb 6, 2018 at 16:29
  • @TomH May I ask how you got the image with the Apache Environment above? Commented May 14, 2018 at 23:30
  • Looks like it's a phpinfo page... mediatemple.net/community/products/dv/204643880/… Commented May 15, 2018 at 0:10
  • Do you know if there is a similar way to set an environment variables from existing header variables? I put up a question on SO but from what I can tell your answer here does something very similar, using server variables instead of headers. Commented Feb 6, 2019 at 18:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.