doujing5937 2013-06-12 18:53
浏览 45
已采纳

环境变量不会从.htaccess传递给PHP

I am trying to pass an environment variable from .htaccess through to PHP. This works just fine on my local WAMP server, but on the server where my website is hosted, it fails without reason.

Here's my test configuration:

.htaccess:

SetEnv TEST_VARIABLE test_value

test.php:

<pre>
getenv('TEST_VARIABLE') = <?php print getenv('TEST_VARIABLE'); ?>

getenv('REDIRECT_TEST_VARIABLE') = <?php print getenv('REDIRECT_TEST_VARIABLE'); ?>
</pre>

On my local server, getting test.php correctly returns:

getenv('TEST_VARIABLE') = test_value
getenv('REDIRECT_TEST_VARIABLE') = 

But on the production server, it returns:

getenv('TEST_VARIABLE') = 
getenv('REDIRECT_TEST_VARIABLE') = 

Things I've ruled out:

  1. mod_env is not installed/enabled by the host. Can't be, because then SetEnv would not be recognized and I'd get a 500 while processing the .htaccess.

  2. AllowOverrides in httpd.conf for this directory doesn't include FileInfo. Can't be, because then Apache would throw an error "SetEnv not allowed here" when encountering the SetEnv directive and I'd get a 500 again.

  3. variables_order in php.ini doesn't include 'E'. This would explain the $_ENV superglobal being empty (which it is), but not why getenv() doesn't return values for these variables.

  4. Entire environment is screwed up. Can't be, because getenv('PATH') and getenv('SERVER_NAME') still return valid values.

At this point I'm at a loss as to what configuration could be causing this.

  • 写回答

2条回答 默认 最新

  • dongxing2302 2013-06-12 18:59
    关注

    I don't think using setenv normally works in .htaccess. It's a shell script command (bash, C shell, etc.). If your server does support setenv, according to https://kb.mediatemple.net/questions/36/Using+Environment+Variables+in+PHP#gs you need to have the variable name start with HTTP_.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛