Articles & Information.

Adding age restriction to an app

Select a topic:

Choose from one of the topics below to browse other articles

Last updated by Rudolph Keown on September 02, 2013 15:25

To add age restriction to your apps, you will have to make use of the User API which allows you access to the core attributes of a Mxit user. You can determine a user’s age and if they are too young to access the content in your app, return them to the home page. (After you have given them an explanation why you are returning them to the home page).


Step 1:

You will need to obtain an OAuth token first, using base64encoded(ClientID:ClientSecret), which is found on the app page in your developer dashboard.

For this example we will use the provided Mxit Wrapper class written by Ashley Kleinhans.


require_once('MxitAPI.php');
try {
 $key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
 $secret = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;
$api = new MxitAPI($key, $secret);  // sending our Client ID and Client Secret to get encoded and set up URLs for API calls.
$api->get_app_token('profile/public');//Sends the desired OAuth scope needed and gets an access Token

Step 2:

You will need to set the scope of the token to be ‘profile/public’. Use the HTTP GET method. If you do not wish to code your own method you can have a look at our Code Examples page and find the appropriate code sample to assist you.


Step 3:

You will need to pass the users’ Mxit ID through to this call, remembering that this information is provided via the Mxit specific headers. All the profile information is returned as a JSON object, so you will need to deal with the returned object as such.


if  (!$api->error)  //ensures only if we do not receive any errors that we proceed.
{
$token=$api->result->access_token;
$profile=$api->get_basic_profile($mxitid); //Obtain the user’s basic profile information in JSON by sending their mxitid obtained via HTTP Headers
 }
else
{
echo "Error\n";
}
 
} catch (Exception $e) {
 echo $e->getMessage();
}


Step 4:

Add the  following line of code to the code above to get the age from the profile and save the information into a variable.

$age=json_decode($profile->Age,true); // gets only the users Age from their profile. 


Step 5:

Check if the user is old enough to view your content, if they are let them proceed, if not inform them they are too young (or too old and) and return them to your home page of your app. It is back practice to explain why users can’t access the content.


if ($age > 18) 
{
 echo '<a href="18plus.php">Proceed</a>';
 }
 else {
 echo 'Sorry you are too young to view this content, your current age is: $age and you need to be over 18 to view this content.<br/> <a href="index.php">Home</a>';  
}


If the user is old enough, they will see the appropriate link in Mxit to proceed:


Figure 1: Example of the link user sees if they are old enough


If the user is too young, they will see the (custom) message you created.


Figure 2: Example of the message a user sees if they are not old enough