【问题标题】:Make a webpage inaccessible to all but my PHP script使除我的 PHP 脚本之外的所有人都无法访问网页
【发布时间】:2013-01-22 18:17:45
【问题描述】:

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

【问题讨论】:

  • 是什么阻止人们浏览 PHP 页面本身返回数据?
  • 您需要做的就是找出您想要使用的授权方法。
  • 您如何建议服务器识别“我的从中获取数据的 PHP 页面”?
  • 假设它被称为mypage.php

标签: php wampserver http-status-code-403


【解决方案1】:

雅达提到的.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!');
}

【讨论】:

    【解决方案2】:

    只允许 index.php 访问

    .htaccess 文件

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

    【讨论】:

    猜你喜欢
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 2015-10-11
    相关资源
    最近更新 更多