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

时间:2021-10-05 23:37:26

I'm trying to print my environment variable defined in .htaccess from PHP script in shell.

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

My tree is:
/.htaccess (in root folder)
/test/index.php (in "test" folder)

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

I set my variable in .htaccess with SetEnv:

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

SetEnv HTTP_TEST "testing"

My PHP script "/test/index.php" is:

我的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

But if I access my PHP script from my browser there is no problems (without #!/usr/bin/php of course...)

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

Thank you for any help.

感谢您的任何帮助。

1 个解决方案

#1


I built a small PHP script that parse the .htaccess file to find the SetEnv

我构建了一个小的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);

#1


I built a small PHP script that parse the .htaccess file to find the SetEnv

我构建了一个小的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);