我难以接受年龄验证,我想知道现在最好的方法是什么。它不一定非常强大,我最初的计划是使用jquery。
为了便于使用,我在这里粘贴了这个方法:
用于需要验证的网页
if ($.cookie('is_legal') == "yes") {
//legal!
} else {
document.location = "http://www.domain.com/[pathtofile]/verify.php?redirect=http://<?php echo $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; ?>";
}
验证页面
$('#accept-btn').click(function(){
$.cookie('is_legal', 'yes', { expires: 1, path: '/', domain: 'domain.com' });
<?php if ( !isset($_REQUEST['redirect']) || $_REQUEST['redirect'] == "" ) { ?>
document.location = "http://www.domain.com";
<?php }else{ ?>
document.location = "<?php echo $_REQUEST['redirect']; ?>";
<?php } ?>
});
这是它的html部分。
<p>Are you OVER 18</p>
<div id="accept-btn">YES</div>
<div id="noaccept-btn"> NO</div >
第一个问题是else语句不起作用,它似乎使用相同的变量来重定向YES选项和NO选项。
所以我很好奇,只是为了学习,为什么这不能正常工作以及如何解决,其次这是更好的方法。就像我说seo / php或强大的方式并不重要。
我希望的结果是,当他们访问网站上的任何页面时,他们会得到是或否选项,然后,当他们可以访问该页面时,如果他们选择否,则是重定向。
答案 0 :(得分:2)
这个纯PHP版本怎么样?
<?php
/**
* @author - Sephedo
* @for - Deedub @ Stackoverflow
* @question - http://stackoverflow.com/questions/18751788/age-verification
*/
$min_age = 18; // Set the min age in years
if( isset( $_POST['submit'] ) )
{
if( mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year'] ) < mktime(0, 0, 0, date('m'), date('j'), ( date('Y') - $min_age ) ) )
{
var_dump("over $min_age");
}
else
{
var_dump("under $min_age");
}
}
?>
<form method="POST" >
<fieldset>
<legend>Enter your age</legend>
<label for="day" >Day:</label>
<select name="day" >
<?php
for( $x=1; $x <= 31; $x++ )
{
if( $x == date("j" ) ) echo "<option selected>$x</option>"; else echo "<option>$x</option>";
}
?>
</select>
<label for="day" >Month:</label>
<select name="month" >
<?php
for( $x=1; $x<=12; $x++ )
{
if( $x == date("m" ) ) echo "<option selected>$x</option>"; else echo "<option>$x</option>";
}
?>
</select>
<label for="day" >Year:</label>
<select name="year" >
<?php
for( $x=date("Y"); $x>=(date("Y")-100); $x-- )
{
echo "<option>$x</option>";
}
?>
</select>
<input type="submit" name="submit" />
</fieldset>
</form>
答案 1 :(得分:0)
评论中要求的新版本
<?php
/**
* @author - Sephedo
* @for - Deedub @ Stackoverflow
* @question - http://stackoverflow.com/questions/18751788/age-verification
*/
// ADD TO ALL PAGES WHICH NEED TO HAVE AGE VERIFICATION
session_start();
if(! isset( $_SESSION['age_verification'] ) or $_SESSION['age_verification'] != true ) die( header("Location: checkage.php?url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]") );
// session_destroy(); // uncomment to reset for testing and debugging.
echo 'hello over 18 person';
?>
<?php
/**
* @author - Sephedo
* @for - Deedub @ Stackoverflow
* @question - http://stackoverflow.com/questions/18751788/age-verification
*/
// checkage.php
session_start();
// checkage.php
if( isset( $_POST['yes'] ) )
{
$_SESSION['age_verification'] = true;
if( isset( $_GET['url'] ) )
{
die( header('Location: ' . $_GET['url'] ) );
}
else
{
die( header('Location: index.php') );
}
}
elseif( isset( $_POST['no'] ) )
{
$_SESSION['age_verification'] = false;
}
// The below line will check if someone has already said no so you can show them a page which tells them to they are too young.
if( isset( $_SESSION['age_verification'] ) and $_SESSION['age_verification'] != true ) die( header('Location: tooyoung.php') );
?>
<form method="POST" >
<fieldset>
<legend>Are you over 18?</legend>
<input type="submit" name="yes" value="Yes" /> <input type="submit" name="no" value="No" />
</fieldset>
</form>