2013-11-13 6 views
-3

Я хочу сделать упрощенный 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'); 
    } 
} 
?> 
+0

_Will this work_ eum попробовать? –

+0

Да, возможно, я мог, только об этом беспокоился: «if ($ year_val> = 19 && $ month_val> = 0 && $ day_val> = 0)« не уверен, что это = 21 или нет, – Terry

+1

Он будет работать для ваших посетителей Клянусь тебе. Если вы задаетесь вопросом, будет ли он работать, препятствуя доступу несовершеннолетних к вашему сайту ... нет. – Damon

ответ

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

Теперь посмотри? Я добавил, что сделал это месяц месяц год – Terry

+0

@Terry: Я исправил код. –

+0

по какой-то причине его дает мне все эти ошибки «Примечание: Undefined индекс: месяц Примечания: Undefined индекс: день Извещения: Undefined индекс: год Примечание: Использование неопределенной константы д - предполагается,„d“ Примечание: Использование неопределенного константа m - принятая 'm' Примечание: использование неопределенной константы Y - предполагается 'Y' " – Terry

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

Здесь Друг: 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> 
+0

Зачем вы смешиваете функцию 'date()' с 'DateTime class'? –

+0

Извините, не понимаю – juniorb2s

+0

Это '$ fim = Date ('d/m/Y', time());' и затем '$ e = DateTime :: createFromFormat ('d/m/Y', $ fim); '. Вы смешиваете 'date()' и 'DateTime'. Зачем? Вы можете достичь этого с помощью ключевого слова 'now' (https://eval.in/66990). –

Смежные вопросы