[PHP][Help] Registration form age verification

Status
Not open for further replies.

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Basically once a user registers i want it to make sure that they are a required age to register, however im unsure how this works, im wanting it to return an error if they are too young.
Parts of my scripts are below

Heres the part of my form:
HTML:
<b>Birthdate:</b><br />
                                <select name="day" id="day">
                                <?php echo $dayOptions; ?> </select>
                                <select name="month" id="month" onChange="updateDays();">
                                <?php echo $monthOptions; ?> </select>
                                <select name="year" id="year" onChange="updateDays();">
                                <?php echo $yearOptions; ?> </select> <br /><br />

The var which inserts into the database(this works and inserts):
PHP:
$dob = "{$_POST['year']}-{$_POST['month']}-{$_POST['day']}";

Im wanting the error to return if the person is under 13 years old

Any help into how i would do this is appreciated

Thanks
 

Alam

shietttt
Jul 3, 2011
433
166
PHP:
if ($dob)<13)
{
  echo "You are too young to register";
}
else
{
....
}

I am not completely sure if $dob will represent the whole age , if it doesn't you at least got the idea of how will the data align.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
This isn't very precise, but what about something like:

PHP:
<?php
if ( $year < 2000 )
{
    echo 'You must be 13 to register to this site.';
}
?>
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,926
3,921
You could do:
PHP:
<?php

$timestamp = strtotime('-13 years');

if($year > date('Y', $timestamp) && $month > date('n', $timestamp) && $day > date('j', $timestamp)) {

    echo 'Y U SUUUUU YOUNG?';

}
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
I ended up recoding it to this:
PHP:
$dob = "{$_POST['year']}-{$_POST['month']}-{$_POST['day']}";
   
    // Has the user filled in Date of Birth?
    $birth = strtotime($dob);
    $min = strtotime('+13 years', $birth);
    if(time() < $min)
      {
        $errors[12] = "You must be atleast 13 years old to register!";
      }

The code alam gave me worked but didnt work as i wanted it to.
However the above code works perfectly
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
I don't understand the $dob variable, when I subtract them dates I get 1970 (1994-6-18).

Also, you don't need to do '$errors[12] = ', you can do '$errors[] = '.
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
I don't understand the $dob variable, when I subtract them dates I get 1970 (1994-6-18).

Also, you don't need to do '$errors[12] = ', you can do '$errors[] = '.
The $dob was orginally used to insert into the database, as the registration form has 3 drop down forms (day/month/year)
I then decided i would use that to get the persons age, and return the error if they are younger

It all works, if i test it out on register, if i put birthday as 6th feb 1999 (not 13 till 2mz) it returns the error
If i put todays date and before (which makes them older) no error returns.
 
Status
Not open for further replies.

Users who are viewing this thread

Top