Age Verification Script

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 50192 Views - Last Post: 17 February 2011 - 03:39 PM Rate Topic: -----

#1 Guest_coleen*


Reputation:

Age Verification Script

Posted 19 August 2010 - 10:49 AM

I am new at php programming.
I have an alcoholic website that i am developing...i have to put an age verification on my welcome page of which a user must be 18years or more before entering site.
The script i am submitting is not my own...but i have tried to use it and i am getting problems with its verifying the age. Also it does not directs you into my site once the age is correct. Please assist with this issue.

      <?php
//define the year
$thisYear = date('Y');
$filtered = array_map("strip", $_POST);

function strip($val){
    $val = strip_tags($val);
    $val = htmlentities($val,ENT_QUOTES);
    return $val;
}

function renderDropDown($name, $opts, $valueIsKey=true){
    $out = array();
    $out[] = '<select name="'.$name.'">';
    foreach($opts as $key => $val){
        if($valueIsKey){
            $out[] = '<option value="'.$key.'">'.$val.'</option>';
        } else {
            $out[] = '<option value="'.$val.'">'.$val.'</option>';
        }
    }
    $out[] = '</select>';

return implode("\n", $out);
}
       
if($_POST['submit'] != 'submit' && !isset($_POST['submit'])){
   //define text months
   for($i=2; $i<=13; $i++){
        $calTime = mktime(0, 0, 0, $i, 0, $thisYear);
        $months[date('m', $calTime)] = date('M', $calTime);
   }
   $renderHTML = true;
} else {    
    //try to construct a valid date from post data
    if(checkdate($filtered['months'], $filtered['days'], $filtered['years'])){
        //valid date..check if they are18+
        $validEighteen = $thisYear - 18;
        if($filtered['years'] <= $validEighteen){
            //inside you go
           die('yes'); 
		} else {
				header('Location: http://www.entersite.html');
		}
	
	} else {
            header('Location: http://www.entersite/reject.html');
        }
    } 
    

if($renderHTML){
?>
    <form action="" method="post" name="ageVerifier" target="_blank">
        Day: <?php print(renderDropDown('days', range(1,31), false)); ?>
        Month: <?php print(renderDropDown('months', $months)); ?>
        Year: <?php print(renderDropDown('years', range($thisYear, $thisYear-100), false)); ?>
        <input type="submit" name="submit" value="submit" />
    </form>
<?php
}
?>



Edited by macosxnerd101: Welcome to DIC! :) Please, :code:.

Is This A Good Question/Topic? -1

Replies To: Age Verification Script

#2 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Re: Age Verification Script

Posted 19 August 2010 - 11:47 AM

To avoid the complication of PHP I would suggest using a simple link. One that says "If you are 18+ Click Here" otherwise "Click Here" and the non-18 link will navigate them away from your site.
Was This Post Helpful? 1
  • +
  • -

#3 Guest_coleen*


Reputation:

Re: Age Verification Script

Posted 19 August 2010 - 12:16 PM

i have used the corrected code that posted by macosxnerd101 and am still not getting the user to be directed into the site..instead the page is printing 'die' instead of entering the site, also the age is not validating properly, when the current year is selected as being the birthdate the user is able to enter the site.
It is my employer's request that i use an age validation.
Was This Post Helpful? 1

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1490
  • View blog
  • Posts: 8,217
  • Joined: 07-September 06

Re: Age Verification Script

Posted 19 August 2010 - 01:17 PM

The code wasn't corrected by macosxnerd101, it was just put in the proper tags so the site would highlight it for people.

I personally would have put the code together a bit differently. I would have done something along the lines of this (NOTE - Untested, may have bugs):
<?php

$minAge = 18;
$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="yyyy/mm/dd" />
    <br />
    <input type="submit" name="submit" value="Verify Age" />
  </form>
OET;

if(isset($_POST['submit'])){
  $age = strtotime($_POST['dob']);
  $eightteen = strtotime("-" . $minAge . " years");
  if($age && $eightteen && $age <= $eightteen){
  // make sure that the variables aren't null, and then that they follow our requirements (age needs to be a smaller number, as such it was longer ago, than eightteen)
    header('Location: http://www.entersite.html');
  }
  else{
    header('Location: http://www.entersite/reject.html')
  }
}
else{
  echo $html;
}
?>


Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,491
  • Joined: 08-August 08

Re: Age Verification Script

Posted 19 August 2010 - 04:27 PM

Another way is to make use of the strtotime function:
<?php
$x = "August 20, 1992";
$then = strtotime($x);
$now = strtotime("now");
$age = $now - $then; // age is in seconds
$eigthteen = 18*3600*24*365.25;  // 18 years in seconds
if($age > $eigthteen)
	echo "Over eigthteen.";
else
	echo "Under age.";
?>

Was This Post Helpful? 0
  • +
  • -

#6 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Re: Age Verification Script

Posted 19 August 2010 - 05:41 PM

CT's a freakin' show off :bigsmile:
Was This Post Helpful? 1
  • +
  • -

#7 coleen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-August 10

Re: Age Verification Script

Posted 20 August 2010 - 09:55 AM

View PostBetaWar, on 19 August 2010 - 07:17 PM, said:

The code wasn't corrected by macosxnerd101, it was just put in the proper tags so the site would highlight it for people.

I personally would have put the code together a bit differently. I would have done something along the lines of this (NOTE - Untested, may have bugs):
<?php

$minAge = 18;
$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="yyyy/mm/dd" />
    <br />
    <input type="submit" name="submit" value="Verify Age" />
  </form>
OET;

if(isset($_POST['submit'])){
  $age = strtotime($_POST['dob']);
  $eightteen = strtotime("-" . $minAge . " years");
  if($age && $eightteen && $age <= $eightteen){
  // make sure that the variables aren't null, and then that they follow our requirements (age needs to be a smaller number, as such it was longer ago, than eightteen)
    header('Location: http://www.entersite.html');
  }
  else{
    header('Location: http://www.entersite/reject.html')
  }
}
else{
  echo $html;
}
?>


Hope that helps.


Thanks BetaWar for your script ...works beautifully except that it does not seem to recognise whether a month has 30 or 31 days..can you assist with this?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,491
  • Joined: 08-August 08

Re: Age Verification Script

Posted 20 August 2010 - 10:10 AM

Using strtotime for everything means you don't need to worry about the days in a month or year. ;)
<?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;
}
?>

Was This Post Helpful? 0
  • +
  • -

#9 coleen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-August 10

Re: Age Verification Script

Posted 23 August 2010 - 08:30 AM

View PostCTphpnwb, on 20 August 2010 - 04:10 PM, said:

Using strtotime for everything means you don't need to worry about the days in a month or year. ;)
<?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;
}
?>

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,245
  • Joined: 27-December 08

Re: Age Verification Script

Posted 23 August 2010 - 08:32 AM

Is there a question?
Was This Post Helpful? 0
  • +
  • -

#11 coleen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-August 10

Re: Age Verification Script

Posted 23 August 2010 - 09:25 AM

View PostCTphpnwb, on 20 August 2010 - 04:10 PM, said:

Using strtotime for everything means you don't need to worry about the days in a month or year. ;)
<?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;
}
?>

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,245
  • Joined: 27-December 08

Re: Age Verification Script

Posted 23 August 2010 - 09:26 AM

coleen, is there a reason you keep quoting CTphpnwb? Do you have a question?

Edit: coleen, please stop quoting other members if you don't have anything to add.
Was This Post Helpful? 0
  • +
  • -

#13 coleen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-August 10

Re: Age Verification Script

Posted 23 August 2010 - 09:42 AM

Sorry about that...Internet Services problem...i have used the last script submitted by CTphpnwb but seems to be getting the same problem with the 30 and 31days in a month.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,245
  • Joined: 27-December 08

Re: Age Verification Script

Posted 23 August 2010 - 09:50 AM

All sins forgiven. Sorry about your Internet problem. :)
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,491
  • Joined: 08-August 08

Re: Age Verification Script

Posted 23 August 2010 - 09:52 AM

Can you show an example? I tested by using birth dates just under and just over 18 years ago and it worked for me, as did dates far greater and lesser than 18 years ago.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2