Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在shell中调用的PHP脚本中访问.htaccess中定义的环境变量?_Php_.htaccess_Shell_Setenv - Fatal编程技术网

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

在shell中调用的PHP脚本中访问.htaccess中定义的环境变量?,php,.htaccess,shell,setenv,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脚本,用于解析.htaccess文件以查找SetEnv #!/usr/bin/php <?php

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

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

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

SetEnv HTTP_TEST "testing"
我的PHP脚本“/test/index.PHP”是:

#/usr/bin/php

我构建了一个小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);
#/usr/bin/php

您是从命令行还是从http请求运行index.php脚本?不可能从命令行运行。因为这里根本不涉及apache,所以在.conf/.htaccess中执行的任何操作都不会产生任何效果。这就像是想知道为什么你站在外面会被雨淋湿,因为你在家里不会被雨淋湿。你在运行mod_env吗@MarcB,这正是我问的原因……你可以运行cron脚本,模拟“从浏览器运行”。基本上,您可以使用
wget
命令调用http请求。这会解决你的问题。
#!/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);