Creating a helper to work with a person's date of birth

From time to time, you'll need an age verification script, a method to ascertain whether or not a user is of a certain age. Based on their age, they may be allowed, or disallowed, from viewing content, for example, a website that promotes adult products, such as alcohol or tobacco or a games site that promotes a game rated for certain ages. The code in this recipe helps you to ascertain a user's age, compares that against a minimum age requirement, and displays an HTML file accordingly.

How to do it…

We're going to create five files:

  • /path/to/codeigniter/application/controllers/register.php: This is the controller for our recipe
  • /path/to/codeigniter/application/helpers/dob_val_helper.php: This file calculates the user's age, compares it to the required age, and returns true or false depending on the result
  • /path/to/codeigniter/application/views/register/signup.php: This file displays the age verification form
  • /path/to/codeigniter/application/views/register/enter.php: This is displayed if the user can enter
  • /path/to/codeigniter/application/views/register/noenter.php: This is displayed if the user cannot enter
  1. Create the controller file, register.php, and add the following code to it:
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'),
    
    class Register extends CI_Controller {
      function __construct() {
        parent::__construct();
        $this->load->helper('form'),
        $this->load->helper('dob_val'),
      }
      public function index() {	
        $this->load->library('form_validation'),
        $this->form_validation->set_error_delimiters('', '<br />'),
        $this->form_validation->set_rules('year', 'Year', 'required|min_length[4]|max_length[4]|trim'),
        $this->form_validation->set_rules('month', 'Month', 'required|min_length[2]|max_length[2]|trim'),
        $this->form_validation->set_rules('day', 'Day', 'required|min_length[2]|max_length[2]|trim'),
    
        if ($this->form_validation->run() == FALSE) {
          $this->load->view('register/signup'),
        } else {
          $dob = array(
            'year' => $this->input->post('year'),
            'month' => $this->input->post('month'),
            'day' => $this->input->post('day')
          );
          $at_least = 18;
          if (are_they_old_enough($dob, $at_least = 18)) {
            $this->load->view('register/enter'),
          } else {
            $this->load->view('register/noenter'),
          }
        }
      }
    }
  2. Create the helper file, dob_val_helper.php, and add the following code to it:
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'),
    function are_they_old_enough($dob, $at_least = 18) {
      $birthday = strtotime($dob['year'].'-'.$dob['month'].'-'.$dob['day']);
      $diff = floor((time() - $birthday) / (60 * 60 * 24 * 365));
    
      if ($diff >= $at_least) {
        return true;
      } else {
        return false;
      }
    }
  3. Create the view file, signup.php, and add the following code to it:
    <?php echo form_open() ; ?>
    
      <?php echo validation_errors() ; ?>
    
      Day <input type="text" name="day" size="5" value="<?php echo set_value('day') ; ?>"/>
    
      Month <input type="text" name="month" size="5" value="<?php echo set_value('month') ; ?>"/>
    
      Year <input type="text" name="year" size="5" value="<?php echo set_value('year') ; ?>"/>
    
      <input type="submit" value="go" />
    
    <?php echo form_close() ; ?>
  4. Create the view file enter.php and add the following code to it:
    <p>You are old enough to view page</p>
  5. Create the view file noenter.php and add the following code to it:
    <p>You are NOT old enough to view page</p>

How it works…

First off, we come to the controller; the controller loads the URL helper (as we're using the redirect() function and a helper we will create called dob_val):

function __construct() {
  parent::__construct();
  $this->load->helper('form'),
  $this->load->helper('dob_val'),
}

We then set up form validation and set the rules for our day, month, and year fields from the HTML. The register/signup.php view file is loaded, ready for the user to enter their date of birth in the three form fields.

The user will press submit, and if the submission passes for validation, the three form values are put into the $dob array:

$dob = array(
	'year' => $this->input->post('year'),
	'month' => $this->input->post('month'),
	'day' => $this->input->post('day')
);

We set our minimum age (the age the user must be in order to view age restricted content) like this:

$at_least = 18;

Then, we pass the $dob array along with the $at_least variable to the dob_val helper:

if (are_they_old_enough($dob, $at_least = 18)) {
  $this->load->view('register/enter'),
} else {
  $this->load->view('register/noenter'),
}

The helper calculates if the user is above or below the $at_least age, returning TRUE if they are above and FALSE if they are not. If they are, they see the register/enter view file, and if they aren't they see the register/noenter view file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.153.69