0

我在做年龄验证时被难住了,我想知道现在最好的方法是什么。它不一定要健壮,我最初的计划是使用 jquery。

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

为了方便使用,我把方法贴在这里:

用于需要验证的页面

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 或稳健的方式并不重要。

我想要的结果是,当他们访问网站上的任何页面时,他们会得到一个是或否选项,然后,当他们访问该页面时,如果他们选择否,那就是重定向。

4

2 回答 2

3

这个纯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>
于 2013-09-11T22:07:52.867 回答
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>
于 2013-09-11T22:14:16.163 回答