问题
I am not very familiar with IIS7 and I need to create a server variable so that I can access it within my PHP code. I can easily accomplish this in Apache by using:
SETENV variable_name variable_value
in the .htaccess file. However accomplishing this in IIS seems to be a bit of a secret. I have looked online for info and the closest thing I can find states using the URL Rewrite Module using these steps:
- Clicking on View Server Variables
- Click the Add link and enter the variable name
- Go back to rules and click the View Rewrite Maps
- Click Add Rewrite Map and enter the default value.
- Restarted the server.
Full docs can be found at: http://www.iis.net/learn/extensions/url-rewrite-module/setting-http-request-headers-and-iis-server-variables
I followed that but it still doesn’t work. Any ideas how this is done? Is it even possible to do this in IIS? All I want to do is set up a server variable with a static value.
example: $_SERVER['MY_VARIABLE']
Thanks.
回答1:
In IIS Manager click on your machine, open the "URL Rewrite" icon from the "IIS" icon group.
Under "Actions" (on the extreme right) click the innocuous little "View Server Variables" link, disregarding "Manage Server Variables"
In the "View Server Variables" screen's "Actions" (on the extreme right) click "Add..."
回答2:
I solved this by using the PHP auto_prepend_file directive. More information can be found here: http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
Just create a file that adds the desired value to the $SERVER array, set that as the auto_prepend_file and you are good to go. This will work with any server, not just IIS.
来源:https://stackoverflow.com/questions/15102024/how-to-add-a-server-variable-in-iis