如何保护 .htaccess 的 php 文件不被 php5 崩溃下载

发布于 2024-09-27 17:00:50 字数 147 浏览 1 评论 0原文

昨晚我对我的网络服务器进行了一些管理更改。我用的是php.更新后 php 处理器出现故障,如果有人访问我的主页,php 页面只会下载并向任何访问者显示专有代码和密码。所以我想知道是否有一种方法可以防止使用 .htaccess 以任何形式下载 php 文件,但仍然允许正常查看文件。

Last night I made some admin changes to my webserver. I use php. The php processor failed after the update and if someone went to my homepage, the php page would simply download and show the proprietary code and password to anyone visiting. So I was wondering if there is a way to prevent any form of download for php files using .htaccess -- but still allow for normal viewing of the files.

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

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

发布评论

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

评论(3

冧九 2024-10-04 17:00:50

开发过程中要遵循的一个好模式是使用最小的初始化文件,该文件调用驻留在 Webroot 之外的实际应用程序。这样,在这种情况下,只会暴露没有关键信息的最小存根。

简化示例:

/
  /app
    critical_code.php
  /webroot
    .htaccess   <- rewrites all requests to index.php
    index.php   <- invokes ../app/critical_code.php (or other files as requested)

A good pattern to follow during development is to use a minimal initialization file, which invokes the actual application which resides outside the webroot. That way only a minimal stub with no critical information is exposed in a case like this.

Simplified example:

/
  /app
    critical_code.php
  /webroot
    .htaccess   <- rewrites all requests to index.php
    index.php   <- invokes ../app/critical_code.php (or other files as requested)
自此以后,行同陌路 2024-10-04 17:00:50

这里的问题是 .htaccess 要么向用户提供文件,要么不提供。您不能告诉它拒绝访问 .php 文件,因为这样在正常使用期间访问也会被拒绝。对于 PHP 处理器未正确运行而言,没有后备行为。

在做这样的大事时,也许值得暂时将网络根目录移动到“正在维护”的网站,以尽可能降低风险。

The trouble here is that either .htaccess is serving your files to the user or it's not. You can't tell it to deny access to the .php files, because then access will be denied during normal use, as well. There is no fallback behavior for the PHP processor simply not running correctly.

Maybe it's worth temporarily moving the web root to point to an "under maintenance" site when doing big things like that, to minimize risk as much as possible.

芸娘子的小脾气 2024-10-04 17:00:50

假设您使用的是 Apache,您的 .htaccess 文件将如下所示。

<FilesMatch ".*\.php">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>
<IfModule php5_module>
    <FilesMatch ".*\.php">
        Allow from all
        Satisfy All
    </FilesMatch>
</IfModule>

第一条规则拒绝访问所有 .php 文件。默认情况下,用户将看到 403(禁止)错误。

如果 PHP5 模块成功加载,第二条规则将生效,授予访问权限。

Assuming you're using Apache, your .htaccess file would look something like this.

<FilesMatch ".*\.php">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>
<IfModule php5_module>
    <FilesMatch ".*\.php">
        Allow from all
        Satisfy All
    </FilesMatch>
</IfModule>

The first rule denies access to all .php files. By default, the user will see a 403 (Forbidden) error.

If the PHP5 module successfully loads, the second rule will take affect, which grants access.

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