PHP Classes
PHP Classes
elePHPant
Icontem

PHP Site Gamification Using a Secret Path - PHP Secret URL Path package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Secret URL Path PHP Secret URL Path   Blog PHP Secret URL Path package blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP Site Gamification...  
  Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)  

Author:

Posted on:

Package: PHP Secret URL Path

The main purpose of gamification is to keep our users entertained in game like activities, so that they stay around longer and come back more often, for instance to your site or your app.

We can accomplish this by setting up a sequence of page visits and rewarding our users if they complete the sequence correctly using the Secret Path class.

Read this article to learn how you can use the PHP Secret Path class to implement a neat game like experience for your users.




Contents

Introduction

The Demonstration code

Initialization and Validation of the Secret Path

The Simple Markup

Conclusion

Introduction

In this article I will be using the Secret Path class as a gamification tool to encourage visitors of a site to follow links along a randomly generated path. This will increase our page views, length of time users stay per visit and hopefully decrease our bounce rate.

The Demonstration Code

I will be using the following code for you to follow along in this simple demonstration. If you wish to test it yourself, you should copy the code into a file named test.php.

<?php
session_start();
include( 'secretpath.class.php' );
if( empty( $_SESSION[ 'secpth' ] ) ) {
    $secpth = new secretPath('link','*',4,9);
} else {
    $secpth = unserialize( $_SESSION[ 'secpth' ]);
}
if( $secpth->validatePath( false ) === true ) {
    $userNotice = 'Congratulations, you completed the path';
    $secpth = new secretPath( 'link', '*', 4,9);
} elseif( !empty($_SESSION['secpth']) ){
    $userNotice = ( $secpth->testUserPath() ) ? 'You are on the right track' : 'Wrong track, try again';
}
$_SESSION['secpth'] = serialize($secpth);
?>
<html>
    <head>
        <title>Secret Path</title>
    </head>
    <body>
        <?php echo ( empty($userNotice) ) ? '' : '<h3>'.$userNotice.'</h3>';?>
        <a href="test.php?link=1">Link 1</a><br>
        <a href="test.php?link=2">Link 2</a><br>
        <a href="test.php?link=3">Link 3</a><br>
        <a href="test.php?link=4">Link 4</a><br>
        <a href="test.php?link=5">Link 5</a><br>
        <a href="test.php?link=6">Link 6</a><br>
        <a href="test.php?link=7">Link 7</a><br>
        <a href="test.php?link=8">Link 8</a><br>
        <a href="test.php?link=9">Link 9</a><br>
        <a href="test.php">No Link</a><br>
<?php
var_dump($secpth->getSecPath());
?>
    </body>
</html>

Initialization and Validation of the Secret Path

For our purposes a secret path is a sequence of user clicks that leads to a reward at the end. Once a user follows an incorrect link in the sequence they have to start over. Please refer to the following code which should be included at the top of each page where we have links in the sequence:

<?php
session_start();
include( 'secretpath.class.php' );
if( empty( $_SESSION['secpth'] ) ) {
    $secpth = new secretPath('link','*',4,9);
} else {
    $secpth = unserialize( $_SESSION['secpth'] );
}
if( $secpth->validatePath(false) === true ) {
    $userNotice = 'Congratulations, you completed the path';
    $secpth = new secretPath('link','*',4,9);
} elseif( !empty( $_SESSION[ 'secpth' ] )) {
    $userNotice = ( $secpth->testUserPath() ) ? 'You are on the right track' : 'Wrong track, try again';
}
$_SESSION['secpth'] = serialize($secpth);
?>

On the first line we are starting our session where we are saving the user interactions, their click sequence.

On the second line we are including the code from the secret path class.

The next block of code is a logic check to see if we have the class object saved in a session already or not. If it has not been saved then we instantiate the class, otherwise we restore the object from the data saved in the session.

The secretPath class is instantiated with a tracking variable parameter, a path parameter, length of the path and the largest integer to use in the value. In this case we are using a tracking variable named 'link' and then using the asterisk to trigger the random generation of path which is 4 links long with values between 1 and 9.

You can increase or decrease the difficulty using a combination of path length and available values. The more of each the more difficult it becomes to solve the correct path.

Our next block in the code is where the work is performed by the class, checking to see if the user is on the correct path or not with the validatePath method and returning true or false to our $validated variable. 

Notice that we have also set the strongReset parameter to false, $secpth->validatePath( false ), which will keep the users progress saved if they follow a link that does not have the tracking variable set.

While this is not secure for user authentication, for our gamification purposes we want to allow users the ability to visit pages not being tracked without losing their progress.

If the validatePath method returns true, then the user has completed the path correctly and we generate a new one, otherwise if there is data saved in the session we evaluate if the user is on the correct path with the testUserPath method.

The last line is where the current state of the class object is saved to the session so that we can start where we left off the next time this initialization and validation code is used.

The Simple Markup

Next we have some simple markup that allows our users to navigate the path. It is assumed that you have a working knowledge of HTML so we will only discuss the parts of interest.

<html>
    <head>
        <title>Secret Path</title>
    </head>
    <body>
        <?php echo ( empty($userNotice) ) ? '' : '<h3>' . $userNotice . '</h3>'; ?>
        <a href="test.php?link=1">Link 1</a><br>
        <a href="test.php?link=2">Link 2</a><br>
        <a href="test.php?link=3">Link 3</a><br>
        <a href="test.php?link=4">Link 4</a><br>
        <a href="test.php?link=5">Link 5</a><br>
        <a href="test.php?link=6">Link 6</a><br>
        <a href="test.php?link=7">Link 7</a><br>
        <a href="test.php?link=8">Link 8</a><br>
        <a href="test.php?link=9">Link 9</a><br>
        <a href="test.php">No Link</a><br>
<?php
var_dump( $secpth->getSecPath() );
?>
    </body>
</html>

We are using the $userNotice variable to store and display the text which notifies the user if they are on the right track or not, or if they have successfully completed the path.

We have provided a link for each key/value pair between 1 and 9. If you recall, we had the class randomly generate a path with a maximum value of 9 so we must provide a link for each possible case.

We also have provided a link which does not include the tracking variable. This 'No Link' link is used to demonstrate a user following a link not included in the game and still retaining their current status along the path.

Notice that all of our links point back to the test.php file, which is done to keep this demonstration simple. You are able to, and should, link to as many different pages as you want. Just keep in mind that each page must include the initialization and validation code along with links for each possible case if it is going to be tracked in the game.

Finally we are displaying the secret path that was randomly generated so that we can easily test the script. You can comment out or remove the var_dump( $secpth->getSecPath() ); statement to test the user interaction when the correct path is not known.

Conclusion

Providing some fun and simple ways for users to interact with our site will keep them entertained and offering a small reward, whatever that may be, for their persistence will keep them coming back for more.

Watching your site stats increase and bounce rates decrease will be your own special reward.

If you liked this article, share it with your colleague developers. Feel free to ask questions posting a comment here.




You need to be a registered user or login to post a comment

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

1. Great for tutorials - Jon Lennryd (2017-01-11 13:03)
In-game tutorials made simple... - 1 reply
Read the whole comment and replies



  Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)  
  All package blogs All package blogs   PHP Secret URL Path PHP Secret URL Path   Blog PHP Secret URL Path package blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP Site Gamification...