通过Age Gate的HTTP引用


HTTP Referrer through Age Gate

我在我的网站上设置了一个年龄门,这样17岁以下的用户就不能进入网站,但我希望那些已经书签了特定链接的人能够在通过年龄门后进入该链接:

这是我的年龄门代码:

<?php
session_start();
if(isset($_SESSION['legal'])) { # Check to see if session has already been set
$url = ($_SESSION['legal'] == 'yes') ? 'index.php' : 'message.php';
header ('Location: ' .$url);
}
// If visitor hasn't gone through the age gate - Age Gate function and Set Session//
if(isset($_POST['checkage'])) {
$day = ctype_digit($_POST['day']) ? $_POST['day'] : '';
$month = ctype_digit($_POST['month']) ? $_POST['month'] : '';
$year = ctype_digit($_POST['year']) ? $_POST['year'] : '';
$birthstamp = mktime(0, 0, 0, $month, $day, $year);
$diff = time() - $birthstamp;
$age_years = floor($diff / 31556926);
if($age_years >= 18) {
$_SESSION['legal'] = 'yes'; 
$url = 'index.php';
} else {
$_SESSION['legal'] = 'no'; 
// If failed the Age Gate go to specific page
$url = 'message.php';
}
header ('Location: ' .$url);
}
?>

我可以添加什么到这段代码,以便如果我想去域/page.php或域/子目录/-年龄门将带我到那里后,我通过它?(我知道我必须使用HTTP Referrer,但我不知道如何包含它)。

编辑添加:我知道有时浏览器不会保留/发送HTTP Referrer,所以我需要为那些不传递该值的人提供解决方案。

编辑:AGE基于表单提交的计算-

$day = ctype_digit($_POST['day']) ? $_POST['day'] : '';
$month = ctype_digit($_POST['month']) ? $_POST['month'] : '';
$year = ctype_digit($_POST['year']) ? $_POST['year'] : '';
$birthstamp = mktime(0, 0, 0, $month, $day, $year);
$diff = time() - $birthstamp;
$age_years = floor($diff / 31556926);

我将以另一种方式设置:让每个页面设置一个$_SESSION变量来指示去哪里:

if (!isset($_SESSION['legal']) || $_SESSION['legal'] == 'no') {
    $_SESSION['target'] = $_SERVER['PHP_SELF'];
    header('Location: message.php');
    return;
}
// continue script execution...

在你的message.php中:

$isLegal = check_age(); // your age checking logic
if ($isLegal && isset($_SESSION['target'])) {
    header('Location: ' . $_SESSION['target']);
} else if ($isLegal) {
    header('Location: index.php');
} else {
    // setup message.php with a validation failed message
}

注意,这只是一种可能的变化,但我建议不要依赖于用户数据,如referrer(一些浏览器扩展甚至明确地取消/修改)。