使用 PHP 解析 .htaccess 文件

发布于 2024-09-16 12:28:30 字数 1281 浏览 2 评论 0原文

我正在使用 Zend Framework,并使用 .htaccess 进行某些设置。我现在正在编写用于调度的命令行脚本(例如 cron)。命令行脚本不会查看 .htaccess 文件,因为 Apache 不提供它们。我想用我的脚本解析 .htaccess 以检索一些设置。以下是我特别感兴趣的行:

SetEnv APPLICATION_ENV development

php_value date.timezone America/New_York

我注意到 PEAR File_HtAccess 包,但它似乎只解决 .htaccess 文件的身份验证部分。


解决方案:(归功于Bamieater< /a>)

用于调试输出的 echo 语句,已从工作代码中删除。

$htaccess = file(realpath(dirname(__FILE__)) . '/.htaccess');
echo '<pre>';
foreach ($htaccess as $line) {
    if (preg_match('/^\s*SetEnv\s+APPLICATION_ENV\s+(.*?)\s*$/', trim($line), $matches)) {
        defined('APPLICATION_ENV') || define('APPLICATION_ENV', $matches[1]);
        echo APPLICATION_ENV . PHP_EOL;
    } elseif (preg_match('/^\s*php_(?:admin_)?value\s+date\.timezone\s+(.*?)\s*$/', $line, $matches)) {
        date_default_timezone_set($matches[1]);
        echo date_default_timezone_get() . PHP_EOL;
    }
}
echo '</pre>';

I am using the Zend Framework, and I use the .htaccess for some settings. I am now writing command line scripts for scheduling (e.g. cron). Command line scripts don't look at the .htaccess file because they're not served up by Apache. I would like to parse the .htaccess with my script to retrieve some settings. Here are the lines I'm specifically interested in:

SetEnv APPLICATION_ENV development

php_value date.timezone America/New_York

I noticed the PEAR File_HtAccess package, but it seems to only address authentication portions of the .htaccess file.


SOLUTION: (with credit due to Bamieater)

echo statements for debug output, removed from working code.

$htaccess = file(realpath(dirname(__FILE__)) . '/.htaccess');
echo '<pre>';
foreach ($htaccess as $line) {
    if (preg_match('/^\s*SetEnv\s+APPLICATION_ENV\s+(.*?)\s*$/', trim($line), $matches)) {
        defined('APPLICATION_ENV') || define('APPLICATION_ENV', $matches[1]);
        echo APPLICATION_ENV . PHP_EOL;
    } elseif (preg_match('/^\s*php_(?:admin_)?value\s+date\.timezone\s+(.*?)\s*$/', $line, $matches)) {
        date_default_timezone_set($matches[1]);
        echo date_default_timezone_get() . PHP_EOL;
    }
}
echo '</pre>';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寒冷纷飞旳雪 2024-09-23 12:28:30

每行读取 .htaccess 文件并使用正则表达式来访问数据。

例如这样的事情:

$line = "php_value date.timezone America/New_York";
$pattern = "@^php_value date.timezone (.*)$@";

if(preg_match($pattern,$line,$matches))
{
    print_r($matches);
}

Read the .htaccess file per line and use a regular expression to access the data.

For example something like this:

$line = "php_value date.timezone America/New_York";
$pattern = "@^php_value date.timezone (.*)$@";

if(preg_match($pattern,$line,$matches))
{
    print_r($matches);
}
拥有 2024-09-23 12:28:30

借用 Bamieater 的一点,这里有一个更完整的函数,您可以将其放入 cron 作业文件的开头,用于设置环境变量并包含 php_prepend_file。

function isCLI() {
    return (PHP_SAPI == 'cli');
}

function loadEnvironmentFromHtaccess($file) {
    $line = file_get_contents($file);

    $pattern = '@SetEnv (?P<env>[^ ]*) (?P<value>[^ \n]*)@';
    preg_match_all($pattern, $line, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
        $_SERVER[$match['env']] = $match['value'];
    }

    $pattern = '@php_value auto_prepend_file (?P<value>[^ \n]*)@';
    if (preg_match($pattern, $line, $matches)) {
        require $matches['value'];
    }
}

if (isCLI()) {
    loadEnvironmentFromHtaccess(dirname(__FILE__).'/../../.htaccess');
}

Borrowing a bit from Bamieater, here's a more complete function you can drop into the beginning of your cron job file that sets the environment variables and includes the php_prepend_file.

function isCLI() {
    return (PHP_SAPI == 'cli');
}

function loadEnvironmentFromHtaccess($file) {
    $line = file_get_contents($file);

    $pattern = '@SetEnv (?P<env>[^ ]*) (?P<value>[^ \n]*)@';
    preg_match_all($pattern, $line, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
        $_SERVER[$match['env']] = $match['value'];
    }

    $pattern = '@php_value auto_prepend_file (?P<value>[^ \n]*)@';
    if (preg_match($pattern, $line, $matches)) {
        require $matches['value'];
    }
}

if (isCLI()) {
    loadEnvironmentFromHtaccess(dirname(__FILE__).'/../../.htaccess');
}
别念他 2024-09-23 12:28:30

解析 .htaccess 尽管很容易做到,但在我看来并不是最好的解决方案。

环境变量可能设置在不同的位置,因此您必须检查所有文件(例如,主 Apache 配置、其他 .htaccesses)。

我建议为 shell 脚本设置单独的环境变量,

export APPLICATION_ENV=staging

我将这些设置保留在服务器的全局属性中(apache config 和 .bashrc),因此所有应用程序都会自动知道它们在哪里,而无需在部署时更改文件。

Parsing .htaccess, despite it is easy to do, is not the best solution in my opinion.

The environment variable may be set in different places, so you'd have to check all the files (eg. main Apache configuration, other .htaccesses).

I'd recommend setting separate environment variable for your shell scripts,

export APPLICATION_ENV=staging

I keep those settings in global properties of the server (apache config and .bashrc), so automatically all the apps know where they are without changing the files upon deployment.

温柔少女心 2024-09-23 12:28:30

答案很晚,但您(或进入此线程的其他人)可能会感兴趣。

我用 PHP 编写了一个小型 .htaccess 解析器,它使人们能够将文件作为 ArrayObject 进行操作。

您可以在此处 (GitHub) 查看该项目。

A late answer but it might interest you (or others that come into this thread).

I wrote a little .htaccess parser, in PHP, that enables one to manipulate the file as an ArrayObject.

You can check the project here (GitHub).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文