php - 用于 mod_php 或 PHP 的 Apache2 API(或其他网络服务器)

标签 php api apache2 webserver

我正在寻找一种使用 API 来操作 PHP 运行的网络服务器的方法。

例如,我希望能够动态添加 mod 重写规则或阻止某些路径的 IP 地址,而所有这些都无需触及 .htaccess 文件。

执行类似 this 的操作PHP 存在吗?

最佳答案

PHP 不可能动态更改 Apache 的配置,尽管正如您所指出的,您可以使用 PHP 编辑配置或 .htaccess 文件重新加载配置。但我认为给你的网络应用程序提供对网络服务器如此大的权力会带来很大的安全风险。

动态阻止 IP 的示例可以通过 mod_rewrite's RewriteMap directive 来完成。基本上,您将为您的网站编写一个 mod_rewrite 规则,该规则将指示 mod_rewrite 查看您的 PHP 脚本可以维护的外部数据源。

mod_rewrite 文档有一个示例:Denying Hosts in a Blacklist

Description:
We wish to maintain a blacklist of hosts, rather like hosts.deny,
and have those hosts blocked from accessing our server.

Solution:
RewriteEngine on
RewriteMap hosts-deny txt:/path/to/hosts.deny
RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND [OR]
RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^ - [F]
##
## hosts.deny
##
## ATTENTION! This is a map, not a list, even when we treat it as such.
## mod_rewrite parses it for key/value pairs, so at least a
## dummy value "-" must be present for each entry.
##

193.102.180.41 -
bsdti1.sdm.de -
192.76.162.40 -

Discussion:
The second RewriteCond assumes that you have HostNameLookups turned on,
so that client IP addresses will be resolved. If that's not the case,
you should drop the second RewriteCond, and drop the [OR] flag from
the first RewriteCond.

您的 PHP 脚本可以维护这些文本文件(也可以使用其他数据存储),并且您可以根据您的特定需求制定任意数量的规则集来动态控制访问。这行得通吗?

关于php - 用于 mod_php 或 PHP 的 Apache2 API(或其他网络服务器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12343776/

相关文章:

php - 按订单选择语句

c# - DHL API 整合

linux - 如何在 Linux Ubuntu 12.04 上安装 Laravel

c++ - 在 apache2 上使用 cgi c++ 处理上传的文件

php - .htaccess 文件有问题

php - 在 Eloquent 模型上还有另一种方法 "setConnection"吗?

PHP使用regex循环查询mysql输出一个excel文件

PHP MySQL 插入具有不同 ID 的多行

javascript - 在 JS 中从 Chrome 扩展程序向我的 Rails API 发出基本请求

java - 放心承载认证