Search
Left Quote    I think there is a world market for maybe five computers.
- Thomas Watson, Chairman of IBM 1943    
Right Quote
 
[login] | [Register]
 

Accept Coupon Codes for Downloads

by: sm0ke
Page: 1 of 3
(View All)

Introduction


This tutorial may be useful if you want to deliver downloads using coupon codes to verify authorization to access the file. This method is in no way secure and should not be run on a production environment but is mereley to introduce a quick and easy method to verify coupon codes and direct customers to the correct file requested.

Create the Tables
Firstly we need to create the tables one to hold our download information and one to house the coupon codes.


In the "file" field were not including the extension so if the download file was called file.zip, the only information entered would be file without the ".zip" extension .

Logging into the database

Here we need to create a new directory under the public directory (root) called "site" open up a text editor (I reccomend metapad) and enter the following information


Save the file as site.inc.php in the newly created site directory.
The above file is quite straighforward, it holds all your database access information and creates 2 functions: db_login() and db_logout() these functions can be called in any script which includes the site.inc.php file to log in and out of the database.

OK, we have the tables created on our database, and a way to log in and out of the database saved in a site.inc.php file in the site directory. You now need to add your products to the downloads table and your coupon codes to the coupon table - I'll let you add these, you may already have a products table in place so you can just ignore the create table and use your existing products database by changing a few variables within the script. Using a form to enter the coupon code

Right, lets pretend the user has selected a file to download, they click to pay with a coupon for a free download - we need to present the user with a form to enter the coupon code to enable the download of the correct file.

In the following file we need to call in the information from the database on the download file requested. so a good link to use to get to enter the code from your products page might be http://www.somesite.com/coupon.php?id={id} Lets take a look at the form


Now we have a way for the user to enter a coupon code to download the selected product, save the file as coupon.php under the root of your site.

Verify code and direct to download

Now the above file will use a form action pointing to "verify.php" sending the hidden download id and coupon code along with it. The following file needs to be saved as "verify.php" under your root directory, this file will grab both the download id field and coupon code from the database, verify that the code and file exist and if either is false (doesnt exist) it will exit, if both download and code are ok the user will be directed to the download script to grab the file and the code will be deleted from the database so it cant be reused.


in the above file the variable $querystring points to the field called "file" on the downloads table and adds the .zip extension to said filename, remember, we said not to store the file extension in the database, thats why. The download script

The verify file will redirect the user to a download script on your server so the file requested can be downloaded. We have the variables of $domain, $querystring and $redirectstr $domain is the location of your download.php script which is what we are about to look at, the domain must contain "?file=" at the end, the $redirectstr joins the domain and file variables together to give the full URL to which the user is directed.

download.php



And thats it - well, not quite. The script isnt secure, its not meant to protect highly private files / personal files from being downloaded but should give you an insight into accepting coupon codes, verifying that code and requested download against a database and allowing the user to download while deleting the coupon from being used again.

The download script here is one i downloaded from somewhere a good year or so ago, if i remember where or who wrote it ill post the credits later. - You can always secure this script and use a more secure download script to boot. but it should give the basics.

1  |  2  |  3  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

39,848 Views
4 Total Comments
3 Rating of 5 (1 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank