年龄验证 [英] Age Verification

查看:75
本文介绍了年龄验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑地进行年龄验证,并且想知道现在最好的方法是什么.它不必一定很健壮,我最初的计划是使用jquery.

I got stumped doing age verification and am wondering what's the best way now. It does not have to be robust, my initial plan was using jquery.

我找到了这个资源, http: //www.techerator.com/2010/09/how-to-perform-age-verification-with-jquery-and-cookies-mmm-cookies/

I found this resource, http://www.techerator.com/2010/09/how-to-perform-age-verification-with-jquery-and-cookies-mmm-cookies/

为了易于使用,我在此处粘贴了该方法:

For ease of use, I have pasted the method here:

用于需要验证的页面

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部分.

here is the html part of it.

<p>Are you OVER 18</p>
<div id="accept-btn">YES</div>
<div id="noaccept-btn">     NO</div >

第一个问题是else语句不起作用,并且它似乎在YES选项和NO选项的重定向中使用了相同的变量.

First issue is that the else statement is not working, as well it seems to be using the same variable for the redirect for the YES option and the NO option.

所以我很好奇,仅出于学习目的,为什么这不能正常工作以及如何解决,其次,这实际上是更好的方法.就像我说的seo/php或健壮的方法并不重要.

SO I am curious, just for learning sake, why this is not working properly and how to fix, and secondly what's really the better way here. Like I said seo/php or a robust way is not critical.

我希望得到的结果是,当他们访问网站上的任何页面时,他们会得到是或否的选项,然后,当是时,他们就可以访问该页面,如果选择了否,那就是重定向.

My desired result is that, when they access any page on the site they get a yes or no option and then, when YES they get access to the page, if they choose no, it's a redirect.

推荐答案

纯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>

这篇关于年龄验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆