Single use access codes

Lets say you need to build an application that provides one time access to some information. It can be some files you let your users download, or some game they want to play, or some video to watch. Instead of distributing some sort of usernames and passwords you want to make this experience as easy as possible and provide access based on a simple numeric code. Such code can sent via email or text messages or handed as scratch card similar to one below.



1. We are going to need a simple database table to store those codes. Besides the code code itself we also need to store the status. Status field is empty if code never been used and contains 'used' otherwise.

2. Add this table to project, enable Add page and proceed to Page Designer. This is the only page we going to need for now. Remove all elements that you don't need to see. We want to make this page as light as possible, we only need a title, an input box and a submit button. Make sure to change labels accordingly.

3. Proceed to Editor, right click on this Add page and make sure it is set as a Landing page.

4. Add the following code to Custom Add event (PHPRunner).

$select = array();
$select["code"] = $values["code"];

$rs = DB::Select("codes", $select);
if ($data = $rs->fetchAssoc() ) {
     if ($data["status"]=="used") {
       $error="Code was used already";
	 } else {
       DB::Exec("update codes set status='used' where id=".$data["id"]);
       $error="Code is valid. Ready to redirect";
       // login and or redirect code goes here 
       }
}
else {
	$error="Invalid code";
}
return false;

5. In this code we use Database API to find if such code exists. If it doesn't - we display the error message. If it exists but was used already - we will display another message. If code exists and wasn't used - we should allow user access to some sort of information. For now we are just saying that code is correct.

6. Next steps. For clarify purposes we don't do anything after code was verified. Here is a sample code that will logon user to your application and redirect them to some other page.

Security::loginAs("user");
header("Location: some_other_page.php");
exit();  

Note: if you add login to your project make sure you also enable Guest login and provide Guest users access to this Add page.

More info:
Database API
Security API

2 comments to Single use access codes

  • Mario Iv├ín

    Nice !!!!!!!!!!!!!!!!

  • Eddie

    I have to say one thing. The developer of this unique software has done an amazing job to always stay on cutting edge of software development. This tool has enabled not just starters, but advanced developers to create applications on the fly. Thank you so much for this great tutorial!

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>