Simple PHP Script That Checks User’s Age and Restricts or Grants Access to Restricted Content A PHP tutorial/download

Building on a script covered in a recent tutorial, Simple PHP Script That Accurately Determines User’s Age (in Years), I’ve updated the second variation of the script to not only determine the user’s age but to also determine whether the user is old enough to see certain content.

The front end still looks the same, like this, minus the top message:

A simple age input PHP script
A simple PHP script that takes the user’s birthdate input and accurately outputs their age (in years.)

Instead of outputting a message, the script will redirect you to either a pre-existing webpage of your choice or to a template in which you can provide the content you’re age-restricting. You can see the new code added to the script below:

The variable section
In this section, you can set the minimum age ($minage), the method you want to use to serve the content to the user who is old enough to see it, and then the url for the pre-existing page if you use method #1.

To be clear, there are two methods. The first one allows you to redirect to a pre-existing page of your choice and the second method is hard-coded to redirect the user to the custom.php page included with the script.

// Edit these variables only!

// Important! Make sure to set the following variable to the age in which the user must be to be approved to see content
$minage = "13";

// The method following approval (if user is old enough) -- 1 = redirect to a pre-existing page/url, 2 = custom code which can be updated in custom.php template
$method = "2";

// method 1 -- if you choose to redirect to another page, set the url in the variable below
$url = "http://kimenders.com"; 
// method 2 -- a reminder -- If you selected 2 as your method, you will need to customize the content template file included with this script

// End of variables section

The other two chunks of code that were added depend on whether the user:
1) has already had their birthday this year (due to the current month being past the birth month)
2) hasn’t had their birthday yet and the current month is their birth month
3) has had their birthday and it is still their current birth month
4) hasn’t had their birthday yet and it isn’t their birth month yet

Chunk for if their birthday has occurred:

                  if($user_age >= $minage) {
                  
                       if($method == '1') {
                       
                       header('Location: '.$url);

                       } elseif($method == '2') {

                       header("Location: custom.php");

                       }

                  } else {

                  echo "Sorry. You are not old enough to view this content.";

                  } 

Chunk for if their birthday hasn’t occurred:

 
                                 if($adjusted_user_age >= $minage) {
                                  
                                 if($method == '1') {
                       
                                 header('Location: '.$url);

                                 } elseif($method == '2') {
     
                                 header("Location: custom.php");

                                 }

                                 } else {

                                 echo "Sorry. You are not old enough to view this content.";

                                 }  

As you can see, the chunks of code are very nearly identical — the only difference coming down to the user_age variable.

This script, just like its preceding version is still quite bare and basic. It can certainly be dressed up with CSS and integrated into an existing site easily. If you’re looking for a simple php script that you can integrate into your site that restricts a user if they’re not old enough to access content, then this would fit that bill.

To download it, click the link below!

If you found this post useful, leave a comment below!

16 comments

  1. Thanks for finally talking about >Simple PHP Script That Checks User’s Age and Restricts or Grants Access to Restricted Content A PHP tutorial/download <Loved it!

  2. I do not even know how I ended up here, however I thought this publish was great. I do not realize who you’re however certainly you’re going to a famous blogger should you are not already. Cheers!

  3. Usually I don’t read post on blogs, but I would like to say that this write-up very compelled me to check out and do it! Your writing style has been surprised me. Thanks, quite great article.

  4. This is very attention-grabbing, You’re an overly skilled blogger. I have joined your rss feed and stay up for looking for more of your magnificent post. Additionally, I’ve shared your web site in my social networks

Leave a Reply

Your email address will not be published. Required fields are marked *