Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
993 views
in Technique[技术] by (71.8m points)

php - Age Verification

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.

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:

Used on pages that need verification

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"]; ?>";
}

The verification page

$('#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 } ?>
});

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 >

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about a pure PHP version of this??

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...