2013-03-25 61 views
1

我有一个年龄门建立在我的网站,让17岁以下的用户无法进入现场,但我想要的人,谁已经收藏特定的链接才能进入该链接经过年龄门后:HTTP推荐通过年龄门

这里是我的年龄门代码:

<?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文件或域/子目录/ - 通过之后,Age Gate会带我去那里吗? (我知道我必须使用HTTP Referrer,但我无法弄清楚如何包含它)。

编辑补充:我知道,有时浏览器不会保存/发送HTTP推荐,所以我需要为那些谁不传递价值的解决方案。

编辑:基于表单提交年龄计算 -

$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); 
+0

如果你只存储在会话你的年龄门,它不会被浏览器会话之间持续存在(即如果他们重新启动他们的浏览器)。您可能会考虑使用cookie或其他方式。 – jchapa 2013-03-25 18:02:01

回答

0

我最好安装这个周围的其他方法:每一页设置一个$_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 
} 

记住,这仅仅是一个可能的变化,但我建议不要依赖于用户数据,如引用(某些浏览器扩展甚至明确取消设置/修改)。