年龄验证代码踢人到老?

时间:2016-04-25 22:30:51

标签: php if-statement verify

我的代码是让人们进去,如果有19岁以上,但如果你老了它不让你输入为什么这个?我有一个人试图今天进入我的网站1958年被踢出去但我可以进入1989年它工作正常

索引页

<!doctype html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8" lang=""> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9" lang=""> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="">
<!--<![endif]-->

<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>510 Vapour</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/responsive.css">
<link rel="stylesheet" href="css/animate.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
</head>
<div class="js">
<body>
  <!-- Preloader section -->
<div id="preloader"></div>
 <!-- Preloader section -->
<div class="container"> 
  <!-- AccessBox section -->
  <div id="accessbox"class="animated bounceInDown"> <img class="profile-img" src="images/logo.png" />
    <h2 class="text-center">Please enter your date of birth</h2>
    <!-- Form section-->
    <form action="php/access.php" method="post" class="access-form">
      <input type="text" name="yy" class="access-input-lg" placeholder="2016" required autofocus>
      <input type="text" name="mm" class="access-input" placeholder="05">
      <input type="text" name="dd" class="access-input" placeholder="10">
      <input type="submit" name="submit"class="access-btn" value="OK" >
      <div id="remember" class="checkbox text-center">
        <label class="text-center">
          <input type="checkbox" value="remember-me">
          Remember me </label>
      </div>
    </form>
    <!-- Form section-->
    <h1 class="text-center"><i class="fa fa-exclamation-triangle red"></i> You must be 19+ to enter this site </h1>
    <p class="text-center">By ENTERING, you are consenting to your Province's Smoking Laws. You may be required to show a valid government issued Photo ID indicating your date of birth at time of delivery or at the Post Office.</p>
  </div>
  <!-- AccessBox section --> 
</div>
<!-- /container --> 
<!-- JS files--> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="js/bootstrap.min.js"></script> 
<!-- Pre Loader--> 
<script type="text/javascript">
jQuery(document).ready(function($) {  

$(window).load(function(){
    $('#preloader').fadeOut('slow',function(){$(this).remove();});
});

});
</script>
</body>
</div>
</html>

access.php

<?php

    $minAge = 19;

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

        if(strlen($_POST['mm'])==1)
        $month = '0'.$_POST['mm'];
        else 
        $month = $_POST['mm'];
        $agevar = $_POST['yy'];

      $age = strtotime($agevar);

      $nineteen = strtotime("-" . $minAge . " years");

      if($age && $nineteen && $age <= $nineteen){

         header('Location: https://www.510vapour.com/main');

      }

      else{

        header('Location: ../error.html');

      }

    }

    ?>

2 个答案:

答案 0 :(得分:0)

这是一个更简单的解决方案:

if (time() < strtotime('+19 years', strtotime($dob))) {
   //User is under 19 years of age
   header('Location: ../error.html');
}else{
   header('Location: https://www.510vapour.com/main');
}

有效$dob

1979-02-2121 Feb 1986

您还可以使用jQuery script来检查用户的年龄。

答案 1 :(得分:0)

您可以使用DateTime个对象来计算此人的确切年龄,并根据该对象选择相应的操作。

构建对象时使用year-month-day格式应该可以正常工作,无论年,月或日提供的位数如何。

$birthdate = new DateTime($_POST['yy'].'-'.$_POST['mm'].'-'.$_POST['dd']);
$now = new DateTime;
$age = $birthdate->diff($now);

if ($age->y >= $minAge){
    header('Location: https://www.510vapour.com/main');
} else {
    header('Location: ../error.html');
}