2016-04-25 57 views
0

我的代碼是讓人如果有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

對所有*控制塊使用花括號,你將會有很多像這樣的錯誤。另外,不要使用strtotime作爲日期數學。它不是爲此而設計的。 –

+1

你爲什麼不檢查一天?用戶可能在本月的最後一天出生,技術上他仍然是18歲。 –

+0

lol對不起,即時通訊新的PHP,所以我半不知道你說什麼,你可以幫忙請 –

回答

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來檢查用戶的年齡。

+0

不要認爲這會對我有用 –

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'); 
} 
+0

對不起,即時通訊新的PHP所以這段代碼去哪裏? –

+0

這可以代替'if(isset($ _ POST ['submit'])){'塊中的所有內容。 –

+0

但是,這是一個很小的例子。你仍然需要做一些事情,比如確保必要的'$ _POST'變量實際上被設置爲有效值,如果不是,則會處理錯誤。 –