使除了我的PHP脚本之外的所有人都无法访问网页


Make a webpage inaccessible to all but my PHP script

除了从中获取数据的PHP页面之外,我如何才能使网页将HTTP 403返回给任何试图访问它的人?如果它有帮助,我正在本地主机上运行 WAMP 服务器。

Yada 提到的 .htaccess 方法是有效的。另一种方法是在 PHP 脚本本身中执行此操作。如果是通过 CLI 运行的 cronjob:

if (!empty($_SERVER['REMOTE_ADDR'])) {
    // If a "remote" address is set, we know that this is not a CLI call
    header('HTTP/1.1 403 Forbidden');
    die('Access denied. Go away, shoo!');
}

或者,如果它是由来自其他 PHP 脚本的浏览器请求触发的,只需验证 IP 是否属于您的/本地:

if ($_SERVER['REMOTE_ADDR'] != '192.168.1.5') { // Or whatever your local IP is
    header('HTTP/1.1 403 Forbidden');
    die('Get out and stay out!');
}

只允许索引.php访问

.htaccess 文件

Order Deny,Allow
Deny from all
Allow from 127.0.0.1
<Files /index.php>
    Order Allow,Deny
    Allow from all
</Files>