SetEnv with php.ini
I initially set up a website on a LAMP server, but my employer needs it moved to a WISP (windows/iis/sql server/php .. I don't think this is a real term?) server, which I have done. I'm using IIS v 6, and I read that there is no SetEnv
equivalent where you can set _SERVER
variables. I rely on these variables for some configuration like the DB user/password and a site key for hashing. I'd strongly prefer to keep these out of the php code for security reasons.
The only solution I can think of is setting such variables with php.ini. I believe you can set a default DB connection, but I'd prefer not to do this anyway. Does php.ini have a SetEnv
equivalent?
If not, is this possible to do with IIS anyway?
For example I want to be able to u开发者_运维百科se a custom _SERVER[db_user]
or _SERVER[site_key]
. In my current .htaccess I have
SetEnv db_user tandu
SetEnv site_key blocked_for_your_protection
You set server variables in your php.ini
file. Check the output of a phpinfo()
command where this file is on your server.
Have a look at the documentation about the php.ini settings.
To edit the _SERVER
settings, you have to set those directly in your server:
Apache: works with htaccess and SetEnv
IIS: http://www.php.net/manual/en/function.putenv.php in your *.php file or in the config:
<serverVariables>
<set name="db_user" value="abc" />
<set name="site_key" value="abc" />
</serverVariables>
See http://forums.iis.net/t/1171675.aspx
You can check your result on your phpinfo page. The variables must occur there!
You can always set a real enviroment variable with Start > Control Panel > System > Environment
and read that. Then the settings are visible to all the software running on the machine, though.
In the PHP script (usually index.php) I use -
// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
This checks the possible hierarchy of places that the value might be stored and defaults to 'production' in the ternary condition as above.
On local ( which runs Apache) this gets it value from the SetEnv APPLICATION_ENV "development" in ../Apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerName foo.local
DocumentRoot "C:\wwwroot\foo"
<Directory "C:\wwwroot\foo">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
SetEnv APPLICATION_ENV "development"
ErrorLog "logs/foo.local-error.log"
CustomLog "logs/foo.local-access.log" common
</VirtualHost>
SetEnv APPLICATION_ENV "development" could also have been placed in .htaccess and I assume that .htaccess gets priority.
When I deploy to the production server the constant APPLICATION_ENV gets its value from IIS root server name Application Settings of the same name (all sites on this server inherit from the root but you can give them their own local environment variable).
The environment variable that can be created Computer->Properties->Advanced System Settings-System Variables seems to get priority. The IIS root. Then Individual sites.
精彩评论