Duke SNIPPED Nukem will only allow 18 and above on his site. It seems like my html page is correct and my php is the problem. Anyone have any answers they would like to share with me? MAY JUSTICE REIGN SUPREME!!!!!

<?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($year_val >= 19) {
 header('Location: [URL]http://www.URL.com/Tour/Tour1.html'[/URL]);
} 
if($month_val >= 0) {
 header('Location: [URL]http://www.URL.com/Tour/Tour1.html'[/URL]);
} 
if($day_val >= 0) {
 header('Location: [URL]http://www.URL.com/Tour/Tour1.html'[/URL]); 
 
} else { 
 header('Location: [URL]http://www.URL.com/notvalidbday.html'[/URL]);
}
?>

:twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted:

Dani AI

Generated

Good instincts all around, but the off-by-one issues you saw come from subtracting Y/M/D fields or dividing seconds by 31,536,000. Leap years, DST boundaries, and month lengths make that unreliable. Use PHP’s date math instead: compute age from two DateTime objects, and validate the submitted date before you compare. Also, always quote format strings like 'Y', 'm', and 'd' (using date('d') etc.). (php.net)

Here is a compact, robust pattern that also addresses ’s cookie reset concern by short-circuiting if a decision cookie already exists, and by setting modern cookie attributes:

<?php
$ADULT = 18;
$yesUrl = '/adult.html';
$noUrl  = '/minor.html';

if (isset($_COOKIE['age_gate'])) {
    header('Location: ' . ($_COOKIE['age_gate'] === 'adult' ? $yesUrl : $noUrl), true, 303);
    exit;
}

$y = filter_input(INPUT_POST, 'year', FILTER_VALIDATE_INT);
$m = filter_input(INPUT_POST, 'month', FILTER_VALIDATE_INT);
$d = filter_input(INPUT_POST, 'day', FILTER_VALIDATE_INT);

if (!$y || !$m || !$d || !checkdate($m, $d, $y)) {
    header('Location: ' . $noUrl, true, 303);
    exit;
}

$dob  = DateTimeImmutable::createFromFormat('!Y-m-d', sprintf('%04d-%02d-%02d', $y, $m, $d));
$age  = $dob->diff(new DateTimeImmutable('today'))->y;
$val  = ($age >= $ADULT) ? 'adult' : 'minor';

setcookie('age_gate', $val, [
  'expires' => time() + 31556926, 'path' => '/',
  'secure' => !empty($_SERVER['HTTPS']), 'httponly' => true, 'samesite' => 'Lax',
]);

header('Location: ' . ($val === 'adult' ? $yesUrl : $noUrl), true, 303);
exit;

Notes: keep all headers before any output, and call exit after redirects. If your form posts, 303 avoids resubmission on refresh/back. The cookie options shown are supported directly by setcookie() and help prevent trivial tampering. (php.net)

Recommended Answers

All 8 Replies

dude you should remove the three conditions and put em in one condition so the code should look something like this

<?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($year_val >= 19&&$month_val >= 0&&$day_val >= 0) {
 header('Location: http://www.URL.com/Tour/Tour1.html');
} 
else {     
 header('Location: http://www.URL.com/notvalidbday.html');
}

?>

Nice!!! Thanks for the help!

no problem man
take care
;)

This script doesn't work! Calculation is off beat

Redirect problem

try this one

<?php
$allowed_age = 19;
$bdate = strtotime($_REQUEST['y'].'-'.$_REQUEST['m']."-".$_REQUEST['d']);
$age = (time()-$bdate)/31536000;
if($age >= $allowed_age) {
	header('Location: http://www.URL.com/Tour/Tour1.html');
	exit;
} 
else {     
	header('Location: http://www.URL.com/notvalidbday.html');
	exit;
}
?>

This works fine. Thanks. I was trying to add set cookie to expand on this, which works, but resets the cookie value each time. I want to check if someone has already entered their age and if so, direct them to the appropriate response page. i.e. - don't want a youth hitting back button and enter different age. I know this is easily circumvented anyways, but ESRB requires it for adult game trailers, etc. Here is what I have so far -

<?php
$allowed_age = 18;
$bdate = strtotime($_REQUEST['year'].'-'.$_REQUEST['month']."-".$_REQUEST['day']);
$age = (time()-$bdate)/31536000;
if($age >= $allowed_age) {
        setcookie('legal', 'yes', time() + 31556926, '/', '.'.$_SERVER['SERVER_NAME']);
        header('Location: ');
        exit;
}
else {
        setcookie('legal', 'no', time() + 31556926, '/', '.'.$_SERVER['SERVER_NAME']);
        header('Location: ');
        exit;
}
?>

Here is what the cookie looks like if I retrieve it -
for minor:
noArray ( [0379923826258bc7cbcfa89cd386917d] => e33f27206e07981d495d11f70581e790 [29e6895b11c34b79d161fe2a6f1702f0] => b7b4ff91eb8090ad2f85d6a2100d3c4e [legal] => no )

for adult:
yesArray ( [0379923826258bc7cbcfa89cd386917d] => e33f27206e07981d495d11f70581e790 [29e6895b11c34b79d161fe2a6f1702f0] => b7b4ff91eb8090ad2f85d6a2100d3c4e [legal] => yes )

Thanks for any help in advance!

I would do something like this:

<?php

function allowed( $y, $m, $d, $min ) {
	$age = date("Y") - $y - (( date("md") < $m.$d ) ? 1 : 0);
        return ( $age >= $min ) ? true : false;
}

if ( allowed($_REQUEST['year'], $_REQUEST['bmonth'], $_REQUEST['day'], 18) )
{
	header('Location: http://www.URL.com/Tour/Tour1.html');
}
else
{
	header('Location: http://www.URL.com/notvalidbday.html');
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.