编写apache环境变量并在php脚本中读取 [英] Write apache environment variables and read them in php script
问题描述
我有一个 index.php 文件,它正在运行一些脚本来发出 mysql 请求.出于明显的安全原因,我想从此脚本中删除 mysql 凭据(主机、密码、用户、数据库),并用一些 apache 环境变量替换它们.
I have an index.php file that is running some script making mysql requests. For obvious security reason, i'd like to remove the mysql credentials (host, password, user, database) from this script and replace them with some apache environment variables.
我尝试使用以下代码行在/etc/apache2/envvars 中创建此类变量:export MYSQL_USER='my_user'
然后我打算使用 getEnv php 函数将其取回,如下所示:getenv('MYSQL_USER')
但这什么都不返回.
I tried to create such variables in /etc/apache2/envvars using following line of code : export MYSQL_USER='my_user'
and then I intend to get it back using getEnv php function like this : getenv('MYSQL_USER')
but this returns nothing.
有什么想法吗?谢谢!
推荐答案
四步:
- 在/etc/apache2/envvars 中:
export MYVAR='value'
- 在/etc/apache2/apache2.conf 中:
PassEnv MYVAR
- 重启apache
- 在 apache2 运行所在的 php 文件中:
echo $_SERVER['MYVAR'];
这篇关于编写apache环境变量并在php脚本中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!