【问题标题】:Prevent CURL to access to specific pages阻止 CURL 访问特定页面
【发布时间】:2017-08-01 23:25:11
【问题描述】:

我的index.php 包括header.phpfooter.php 这样一来:

define("LEGAL_PATH", TRUE);
include("header.php");
// ...
include("footer.php");

我包含的每个文件都以:

if (!defined("LEGAL_PATH"))
{
    header("location: index.php");
    exit(0);
}

目前,当我们加载header.php(或footer.php)时,我们会被重定向到index.php 页面。当我们尝试 curl header.php 时,它会返回一个空白页(感谢 exit(0))。

问题:

关于坏人,我想生成一个 404 错误(例如,当我们尝试访问 efjiozfjoijefiojzeof.php 时)即使我们使用 curl 加载页面:在这种情况下(使用 curl 命令),标题位置是非功能性的。

我的 curl 输出(没有exit(0)时):

$> curl https://www.mywebsite.com/header.php
NOBODY SHOULD READ THIS DIRECTLY FROM HEADER.PHP FILE

我目前拥有的 curl 输出(当有 exit(0) 时):

$> curl https://www.mywebsite.com/header.php
$>

我想要的 curl 输出:

$> curl https://www.mywebsite.com/header.php
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /headzefjzefjp.php was not found on this server.</p>
</body></html>
$>

我的想法是包含 404 页面文件,但是当我在详细模式下 curl 时,我仍然有 200 错误代码(而不是 404):

$> curl https://.../header.php -v
...
> GET /header.php HTTP/1.1
> Host: ...
> User-Agent: curl/7.51.0
> Accept: */*
> 
< HTTP/1.1 200 OK
...

那么,真正忽略 header.php 文件(来自浏览器、curl 等)的好方法是什么?

【问题讨论】:

  • header("HTTP/1.0 404 Not Found"); - 必须在显示任何内容之前调用。

标签: php html curl


【解决方案1】:

防止人们访问他们不应该访问的页面(如页眉、页脚或某些库)的正确方法是将它们放在文档根目录之外。

例如,您的文件夹结构可能是:

documentroot/index.php
header.php
footer.php

你的index.php 看起来像:

<?php 
require('../header.php');
echo "some content";
require('../footer.php');

这是阻止用户访问他们不应该访问的页面的最简单、最安全的方法,而不是尝试返回 404 错误。

【讨论】:

    【解决方案2】:

    您需要手动发送404 Not Found。请参阅此答案 How can I create an error 404 in PHP?

    另外,如果您要重定向,则需要设置 301 或 302 状态。 301 or 302 Redirection With PHP 使用“位置”标头时您正在隐式执行的操作

    另一件事是确保 cURL 不遵循 30x 重定向。 (也许你的 curl 是 curl -l 的别名) 尝试使用curl --max-redirects=0 https://www.mywebsite.com/header.php 看看是不是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2017-09-02
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多