Php 通过年龄门的HTTP推荐人

Php 通过年龄门的HTTP推荐人,php,http-referer,Php,Http Referer,我在我的网站上设置了一个年龄门,这样17岁以下的用户就不能进入该网站,但我希望已经为特定链接添加书签的用户能够在通过年龄门后进入该链接: 这是我的年龄门代码: <?php session_start(); if(isset($_SESSION['legal'])) { # Check to see if session has already been set $url = ($_SESSION['legal'] == 'yes') ? 'index.php' : 'message.ph

我在我的网站上设置了一个年龄门,这样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);
}
?>

我将以另一种方式进行设置:让每个页面设置一个$\会话变量,以指示要转到的位置:

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


请记住,这只是其中一种可能的变化,但我建议不要依赖用户数据,如推荐人。一些浏览器扩展甚至明确取消设置/修改这些数据。

如果您只在会话中存储年龄门,那么它将不会在浏览器会话之间保持,即如果他们重新启动浏览器。你可能想考虑使用Cookie或其他一些方法。我不需要让它持久化,在浏览器关闭后,它应该清除,或者有人清除其缓存/ cookies。我并没有运行成人网站或任何东西,只是一个小检查,因为我并没有收集数据。
if (!isset($_SESSION['legal']) || $_SESSION['legal'] == 'no') {
    $_SESSION['target'] = $_SERVER['PHP_SELF'];
    header('Location: message.php');
    return;
}
// continue script execution...
$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
}