PHP年龄检查-拦截和重定向

PHP年龄检查-拦截和重定向,php,redirect,Php,Redirect,我正在开发一个网站,其中涉及一些有关酒精的信息,客户要求访问此页面的年龄检查。我知道它们是多么的毫无用处,但我认为有一些相关的规定 经过一些研究,我发现PHP是进行年龄检查的更好方法,因为禁用javascript会使基于JS的检查变得无用。但是我的PHP知识不是很好。我在这里找到了一个答案,为我指明了正确的方向: 现在的问题是,它不能正常工作 过程如下: <?php $min_age = 18; // Set the min age in years if( isset( $_POST

我正在开发一个网站,其中涉及一些有关酒精的信息,客户要求访问此页面的年龄检查。我知道它们是多么的毫无用处,但我认为有一些相关的规定

经过一些研究,我发现PHP是进行年龄检查的更好方法,因为禁用javascript会使基于JS的检查变得无用。但是我的PHP知识不是很好。我在这里找到了一个答案,为我指明了正确的方向:

现在的问题是,它不能正常工作

过程如下:

<?php

$min_age = 18; // Set the min age in years

if( isset( $_POST['submit'] ) )
{
$_SESSION['age_verification'] = true;

    if( mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year'] ) < mktime(0, 0, 0, date('m'), date('j'), ( date('Y') - $min_age ) ) )
    {
        if( isset( $_GET['url'] ) )
        {
            header('Location: ' . $_GET['url'] ); 
        } 
    }
    else
    {
        header('Location: index.php');
    }
}
else 
{
$_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>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>
  • 用户访问brands.php页面。PHP检查年龄验证会话数据,如果尚未完成检查,则重定向到checkage.PHP:

    <?php 
    include_once("includes/header.php");
    
    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包含用于检查年龄(不可预测)的表单。我将作者的两个答案结合在一起,得出我认为应该可行的结论。我放置了一个if语句来获取品牌页面的url,如果年龄合适,则重定向到那里,否则重定向到主页。该页面的代码如下所示:

    <?php
    
    $min_age = 18; // Set the min age in years
    
    if( isset( $_POST['submit'] ) )
    {
    $_SESSION['age_verification'] = true;
    
        if( mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year'] ) < mktime(0, 0, 0, date('m'), date('j'), ( date('Y') - $min_age ) ) )
        {
            if( isset( $_GET['url'] ) )
            {
                header('Location: ' . $_GET['url'] ); 
            } 
        }
        else
        {
            header('Location: index.php');
        }
    }
    else 
    {
    $_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>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>
    
    
    输入你的年龄
    日期:
    月份:
    年份:
    
这个过程正是我想要的:拦截试图访问页面的用户>重定向到年龄检查>转到页面或返回主页,具体取决于输入。我只是不知道怎么了

抱歉问了这么长的问题,请尽量说清楚。非常感谢您的帮助!虽然我想我正在做一些非常明显和愚蠢的事情…

你错过了课程\u start()


checkage.php还需要session_start();我没有看到这条线。你错过了吗?您可以使用var_dump($_SESSION)转储会话值,以检查是否正确设置了变量。这非常简单。就我而言,这只是人为的错误。谢谢你让我的一天少了很多痛苦!你是说你错过了会话_start(),还是用var_dump解决了这个问题?对不起,我错过了checkage.php中的会话_start()!好的,然后检查我的答案作为解决方案!再次感谢你的帮助!我会马上检查答案,除非你把它贴在评论中:)我的联系人页面和表单处理页面上有session_start()。这似乎也在重置checkage页面的会话,这意味着无论何时您进入contact页面并返回brands页面(检查发生时),会话也会在那里重新启动。有什么办法可以解决这个问题吗?我使用了session_write_close();而不是会话_destroy();在联系人页面的末尾,现在一切似乎都在运行。