Skip to content

Instantly share code, notes, and snippets.

@beaucollins
Created May 31, 2011 17:51
Show Gist options
  • Save beaucollins/1000959 to your computer and use it in GitHub Desktop.
Save beaucollins/1000959 to your computer and use it in GitHub Desktop.
<?php
// Checks an array for a given $key, if it exists, returns the value stored at that
// key. If it doesn't exist it returns the $default value.
function array_var($array, $key, $default = false){
if (array_key_exists($key, $array)) {
return $array[$key];
}else{
return $default;
}
}
// $_REQUEST has both $_GET and $_POST variables stored as an associative array
// Get the sumbmitted year, mont, day, default to 05-FEB-1983 (just using my birthday
// for testing values). Month names and numbers also work as values and the date will
// be parsed correctly by the strtotime function.
$year = array_var($_REQUEST, 'year', '1983');
$month = array_var($_REQUEST, 'month', 'FEB');
$day = array_var($_REQUEST, 'day', '5');
// Create the string that we're going to parse into a date
$submitted = "{$day}-{$month}-${year}";
// Turn the string into a date 'object' (in php it's just the seconds since the epoch)
$date = strtotime($submitted);
// Store 'now' in seconds since the epoch
$now = time();
// Calculate 21 years in seconds
$minimum_age = 21 * 365 * 24 * 60 * 60; // 21 years in seconds
// if the submitted date minus now (in seconds) is greater than
// or equal to 21 years in seconds ($minimum age) then redirect to the over 21 URL
// if not then redirect to under 21 URL.
if ($now-$date >= $minimum_age) {
// You are older than 21
setcookie('age','over21');
header("Location: http://www.oldenough.com");
}else{
setcookie('age','under21');
header("Location: http://www.notoldenough.com");
}
<?php
// Form wasn't submitted, so check for the correct age
if ($_COOKIE['age'] == 'over21') {
// they have been checked already they can view the site so don't do anything
}else if($_COOKIE['age'] == 'under21'){
// they aren't old enough
header("Location: http://wildturkey.ravennainteractive.com/under21.php");
}else {
// doesn't look like they've been checked
header("Location: http://wildturkey.ravennainteractive.com/index.php");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment