创建年龄验证主页

时间:2013-11-13 11:51:30

标签: php

我想制作一个简洁的PHP脚本,这是我的主页。我希望用户输入他们的DOB或从下拉日历中选择它,然后根据他们输入的内容我可以检查他们是否是21。如果不是,我会将它们发送到一个页面,如果是,我会将它们发送到另一个页面。这是我到目前为止所拥有的。

    <div id="age">
You must be 18 or older to view this site.<br>
  You must be 18 or older to view this site.<br>
    <form action="#" method="post">
    Please input your date of birth:
    <input type="text" name="month">
<input type="text" name="day">
<input type="text" name="year">
    <input type="submit" name="submit" value="Verify Age">
    </form>
</div>
        <p></p>

        <?php include "footer.php" ?>
<?php
$month = $_REQUEST['bmonth'];
$day = $_REQUEST['day'];
$year = $_REQUEST['year'];
$this_day = date(d);
$this_month = date(m);
$this_year = date(Y);
$day_val = $this_day - $day;
$month_val = $this_month - $month;
$year_val = $this_year - $year;
if (!empty($_POST['submit'])) {
    $today = date_create('today');
    $birth = date_create("{$_POST['year']}-{$_POST['month']}-{$_POST['day']}");
    $age = date_diff($today, $birth)->y;

    if ($age >= 18) {
        header('Location: home.php');
    } else { 
        header('Location: contact.php');
    }
}
?>

3 个答案:

答案 0 :(得分:0)

<?php

$minAge = 18; // You might read this from a file/database.

$minAge *= 3600*24*365.25;  // $minAge in seconds



$html = <<< OET

  You must be 18 or older to view this site.

  <br />

  <form action="#" method="post">

    Please input your date of birth:

    <input type="text" name="dob" value="" />

    <br />

    <input type="submit" name="submit" value="Verify Age" />

  </form>

OET;



if(isset($_POST['submit'])){

    $birth_date = strtotime($_POST['dob']);

    $now = strtotime("now");

    $age = $now - $birth_date; // age is in seconds

    if($age > $minAge)

        echo "Over minimum age."; // You could use header here.

    else

        echo "Under minimum age."; // You could use header here.

} else

{

    echo $html;

}

答案 1 :(得分:0)

试试这个:

if (!empty($_POST['submit'])) {
    $today = date_create('today');
    $birth = date_create("{$_POST['year']}-{$_POST['month']}-{$_POST['day']}");
    $age = date_diff($today, $birth)->y;

    if ($age >= 18) {
        header('Location: example1.php');
    } else { 
        header('Location: example.php');
    }
}

答案 2 :(得分:-1)

这位朋友:https://gist.github.com/juniorb2ss/a21d5949a91503b9a417

任何问题只是问;)

if(!empty($_POST))
{
 $d = $_POST['d'];
 $m = $_POST['m'];
 $y = $_POST['y'];
 $inicio = "$d/$m/$y";
 $fim = Date('d/m/Y', time());

 $b = DateTime::createFromFormat('d/m/Y', $inicio);     
 $e = DateTime::createFromFormat('d/m/Y', $fim);     
 $intervalo = $b->diff($e);
 $f = (array)$intervalo;

 if ($f['y'] >= 18) 
 {
     print $intervalo->format('You have %Y years');
     //header('Location: example1.php');
 } 
 else 
 { 
     print $intervalo->format('You have %Y years');
     //header('Location: example.php');
 }
}

形式:

You must be 18 or older to view this site.<br>
<form action="#" method="post">
Please input your date of birth:
<input type="text" size="2" name="d" value=""> 
/ <input size="2" type="text" name="m" value=""> 
/ <input size="2" type="text" name="y" value="">
<input type="submit" name="submit" value="Verify Age">
</form>