I've been trying to get this to work properly, but I'm getting an infinite redirect when executed. I've done this in the past, but can't remember exactly how I did it (and I've lost my original files a long time ago).
I have an age gate set up to allow only those over 18 to access content. If they come to my top page (index.php) they'll be asked to enter their birthday (mm-dd-yyyy), and if they meet the age, they will be taken to the next page (home.php). If they aren't of age, then they should be direct to a "sorry" page (message.php). And, if for some reason, they go straight to home.php, they need to be redirected to index.php in order to enter their birthday.
So flow: index.php - home.php (if they pass the age gate)
index. php - message.php (if they don't)
home.php - index.php (if they haven't gone through the age gate yet)
home.php - message.php (if they have gone through the age gate, but failed the check, and are trying to see if they can directly access the page)
(extra) file.php - index.php - file.php (if they haven't gone through the age gate, get redirected to index to do so, then be brought back to file.php once they are pass/checked)
Right now, im getting an infinite loop on the home.php (too many redirects)
my code below:
index.php
<?php
session_start();
if ( isset( $_SESSION[ 'legal' ] ) ) { # Check to see if session has already been set
$url = ( $_SESSION[ 'legal' ] == 'yes' ) ? 'home.php' : 'message.php';
header( 'Location: ' . $url );
}
// If visitor hasn't gone through the age gate - Age Gate function and Set Session//
if ( isset( $_POST[ 'checkage' ] ) ) {
$day = ctype_digit( $_POST[ 'day' ] ) ? $_POST[ 'day' ] : '';
$month = ctype_digit( $_POST[ 'month' ] ) ? $_POST[ 'month' ] : '';
$year = ctype_digit( $_POST[ 'year' ] ) ? $_POST[ 'year' ] : '';
$birthstamp = mktime( 0, 0, 0, $month, $day, $year );
$diff = time() - $birthstamp;
$age_years = floor( $diff / 31556926 );
if ( $age_years >= 18 ) {
$_SESSION[ 'legal' ] = 'yes';
$url = 'index.php';
} else {
$_SESSION[ 'legal' ] = 'no';
// If failed the Age Gate go to specific page
$url = 'message.php';
}
header( 'Location: ' . $url );
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Check</title>
</head>
<body>
<form method="post" action="index.php" id="checkage">
<label for="Month">Month:</label>
<input type="text" name="month" id="month"/>
<label for="Day">Day:</label>
<input type="text" name="day" id="day"/>
<label for="Year">Year:</label>
<input type="text" name="year" id="year"/>
<input type="submit" name="checkage" value="Submit" class="submit-button"/>
</form>
</body>
</html>
home.php
<?php
if (!isset($_SESSION['legal']) || $_SESSION['legal'] == 'no') {
$_SESSION['target'] = $_SERVER['PHP_SELF'];
header('Location: index.php');
return;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
<body>
<p>you are here</p>
</body>
</html>
message.php
<html>
<head>
<meta charset="utf-8">
<title>Sorry</title>
</head>
<body>
<p>sorry you are not able to see this content</p>
</body>
</html>
Unfortunately, I don't have an external server to test on (or link to)