如何检查用户是否试图在没有数据发送到服务器的情况下手动访问脚本?

How to check if user is trying to access script manually with no data sent to the server?

假设我有一个名为 test.php 的脚本,我想知道如何检查用户是否正在尝试访问 example。com/test.php 手动,如果是这样抛出禁止错误 var_dump(http_response_code(403)); 这是我的代码:

if($conditions)
     var_dump(http_response_code(403));

这是我希望 $conditions 是:

$条件=$NOT_LOGGED || ( !isset($_POST) && !isset($_GET) )

问题是,据我所知,用户只是访问 example.com/test.php 是一个 $_GET...所以这段代码会失败.我想要的是一种防止犯规的方法,以确保用户确实在尝试手动进行(是的,我完全知道用户可以手动执行此操作,例如 example.com/test.php?but_thats=not_my_point。有人可以帮我吗? tyvm!

非常简单:检查所有必须满足的条件才能使请求有效,如果不符合,则每一个都失败。

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    header('HTTP/1.0 405 Method Not Allowed');
    exit();
}

if (/* user is not logged in */) {   // fill in your user authentication here
    header('HTTP/1.0 403 Forbidden');
    exit();
}

if (!isset($_GET['foo'])) {
    header('HTTP/1.0 400 Bad Request');
    exit();
}

echo 'Hi! ', $_GET['foo'];