在shell中调用的PHP脚本中的.htaccess中定义的访问环境变量?

时间:2015-05-11 16:59:46

标签: php .htaccess shell setenv

我试图在shell中的PHP脚本中打印.htaccess中定义的环境变量。

我的树是:
/.htaccess(在根文件夹中)
/test/index.php(在" test"文件夹中)

我使用 SetEnv 在.htaccess中设置我的变量:

SetEnv HTTP_TEST "testing"

我的PHP脚本" /test/index.php"是:

#!/usr/bin/php
<?php
echo $_ENV['HTTP_TEST']; // print empty
echo $_SERVER['HTTP_TEST']; // print empty
echo getenv('HTTP_TEST'); // print empty

但是,如果我从浏览器访问我的PHP脚本没有问题(当然没有#!/ usr / bin / php ...)

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我构建了一个小的PHP脚本来解析.htaccess文件以找到SetEnv

#!/usr/bin/php
<?php

// Read all lines from file
$htaccess = file('/.htaccess');

foreach ($htaccess as $line) {
    // Trim left/right spaces, replace all repeated spaces by 1 space
    $line = preg_replace('/[ \t]+/', ' ', trim($line));

    // It's our variable defined with SetEnv
    if (substr($line, 0, 7) === 'SetEnv ') {
        $tmp = explode(' ', substr($line, 7), 2);
        $ENV[$tmp[0]] = str_replace('"', '', $tmp[1]);
    }
}

print_r($ENV);