如何使页面只能从将用户重定向到它的页面访问?

How can i make a page accessable only from a page that redirects the user to it?

我想让一个页面只能从重定向到它的页面访问。这个重定向到这个页面的页面叫做 /purchase.php,然后这个页面重定向到一个叫做 /username.php 的页面,我希望这个页面只能从 /purchase.php 访问,而不是直接来自 url.

解决方法: 对于 purchase.php:

<?php
session_start();
//Put this when the purchase is vailidated
$SESSION_['fromMain'] = "true";
//Then redirect
header ("Location: url.com/username.php");
?>

对于username.php:

<?php
//Check if the browser comes from purchase php
if($_SESSION['fromMain'] == "false"){
//If not redirect to index page
header ("Location: url.com/index.php
} else {
$SESSION_['fromMain'] = "false";
{
?>

作为附加措施(除了 CD001 已经做出的评论),您可以使用 $_SERVER["HTTP_REFERER"] 变量,如果直接从浏览器界面请求脚本,该变量将为空。