Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-16-2010, 08:01 PM   PM User | #1
nockemout
New Coder

 
Join Date: Dec 2008
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
nockemout is an unknown quantity at this point
Generate PIN codes for shopping cart solution

Hello,

I have a project that I am looking to start soon.

I need to be able to generate unique PIN codes (lots of em) which will be distributed via flyers / "credit cards" to potential clients. The client then logs on to my site and puts the code in, which directs them to a purchase page for a downloadable product.

I've looked around at some shopping cart providers, and none seem to have this functionality out of the box, but some come close with an add on - instead of being able to provide serials to users, these shopping cart scripts send the code to the user once the downloadable product is bought, rendering the marketing idea useless.

So, I am contemplating writing up my own PHP script that accesses a SQL db full of PINs and relevant redirect addresses to the product page via a search query.

Can anyone offer me guidance to this solution? I just got O'Riely's php book that I am reading (have been working with php for years now but don't know how to code it myself yet), and I would greatly appreciate any suggestions!

Last edited by nockemout; 03-16-2010 at 08:15 PM..
nockemout is offline   Reply With Quote
Old 03-16-2010, 08:18 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
What are the filetypes and sizes of the downloadable files?
mlseim is offline   Reply With Quote
Old 03-16-2010, 08:22 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Generation of unique data is not the most difficult of tasks. Its a matter of ensuring that the length is proper; even using something like a UUID version 4 is randomized, but the number of potential combinations really minimizes the effects of collisions. That's a 128bit representation, so that provides what, 3 x 10^38 combinations? Thats huge.
I think the pear has a UUID generation class within it. If not, just wiki up uuid generation and code you're own V4 generator in PHP with pack. Thats the absolute easiest way I can think of to create non-incremental low collision identifiers.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
nockemout (03-16-2010)
Old 03-16-2010, 08:23 PM   PM User | #4
nockemout
New Coder

 
Join Date: Dec 2008
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
nockemout is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
What are the filetypes and sizes of the downloadable files?


For now it's .mp3, 3-6mb

Last edited by nockemout; 03-16-2010 at 08:25 PM..
nockemout is offline   Reply With Quote
Old 03-16-2010, 08:25 PM   PM User | #5
nockemout
New Coder

 
Join Date: Dec 2008
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
nockemout is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Generation of unique data is not the most difficult of tasks. Its a matter of ensuring that the length is proper; even using something like a UUID version 4 is randomized, but the number of potential combinations really minimizes the effects of collisions. That's a 128bit representation, so that provides what, 3 x 10^38 combinations? Thats huge.
I think the pear has a UUID generation class within it. If not, just wiki up uuid generation and code you're own V4 generator in PHP with pack. Thats the absolute easiest way I can think of to create non-incremental low collision identifiers.
Thank you! That is very helpful information.
nockemout is offline   Reply With Quote
Old 03-16-2010, 08:51 PM   PM User | #6
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
UUID generator function.

Code:
function generate_uuid()
{
        $chars = array('a', '8', 'b', '9');

        $base = '%04x%04x-%04x-4%s-%s%s-%04x%04x%04x';

        return sprintf($base,
                mt_rand(0, 65535),
                mt_rand(0, 65535),
                mt_rand(0, 65535),
                substr(sha1(mt_rand(2000, 65535)), mt_rand(0, 15), 3),
                $chars[rand(0, 3)],
                substr(sha1(mt_rand(3000, 65535)), mt_rand(20, 35), 3),
                mt_rand(0, 65535),
                mt_rand(0, 65535),
                mt_rand(0, 65535));
}
MattF is offline   Reply With Quote
Old 03-16-2010, 10:24 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
You store all of your MP3 files in one directory ... tucked away.

When someone buys a file, you assign a random code ...
where the code AND the real filename is added to a row in your database,
associated with the user's email, or account (however you do your database).

You email them with a link to a PHP script and the code as a PHP variable.
Your PHP script will serve them the file without giving away the location.
You can then choose to delete that row in your database, or use some sort
of date/time thing that allows them a certain amount of time to do it again.

Here's an example of serving a PDF file to a user with a code:
http://www.catpin.com/pf.php?file=8uj321

I can choose to remove that code from my database once you click the link,
or after a certain number of clicks, or after a certain amount of time.
Each user that purchases that file would be given a unique random code,
but it's really the SAME file, no matter what code is used. And all of my
PDF files are in the same directory, using their original filenames.
The random code is only used to "connect" between the user and the file.

It can be any filetype, PDF, MP3, JPEG, etc.

The only thing you can't control is the user giving copies of it to other people.


Here is the script for serving the PDF files I have ... sort of a summary of the script:
PHP Code:
<?php

// get the random number from the URL
$number=$_GET['file'];

if(
$number){

// here I open my database and match the number to the filename.
// $file = the name of the PDF file.

$file=rtrim($file);

// Your $path would be your own website's path to your files ...

$path "/home/users/web/b75/ipw.yoursite/public_web/myvideos/".$file// the file made available for download via this PHP file
$mm_type="application/octet-stream"// modify accordingly to the file type of $path, but in most cases no need to do so

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");

readfile($path); // outputs the content of the file

exit();
}
else{
header ("location: http://www.yoursite.com");
}
?>



.

Last edited by mlseim; 03-16-2010 at 10:39 PM..
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
nockemout (03-17-2010)
Old 03-17-2010, 12:15 AM   PM User | #8
nockemout
New Coder

 
Join Date: Dec 2008
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
nockemout is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
You store all of your MP3 files in one directory ... tucked away.

When someone buys a file, you assign a random code ...
where the code AND the real filename is added to a row in your database,
associated with the user's email, or account (however you do your database).

You email them with a link to a PHP script and the code as a PHP variable.
Your PHP script will serve them the file without giving away the location.
You can then choose to delete that row in your database, or use some sort
of date/time thing that allows them a certain amount of time to do it again.

Here's an example of serving a PDF file to a user with a code:
http://www.catpin.com/pf.php?file=8uj321

I can choose to remove that code from my database once you click the link,
or after a certain number of clicks, or after a certain amount of time.
Each user that purchases that file would be given a unique random code,
but it's really the SAME file, no matter what code is used. And all of my
PDF files are in the same directory, using their original filenames.
The random code is only used to "connect" between the user and the file.

It can be any filetype, PDF, MP3, JPEG, etc.

The only thing you can't control is the user giving copies of it to other people.


Here is the script for serving the PDF files I have ... sort of a summary of the script:
PHP Code:
<?php

// get the random number from the URL
$number=$_GET['file'];

if(
$number){

// here I open my database and match the number to the filename.
// $file = the name of the PDF file.

$file=rtrim($file);

// Your $path would be your own website's path to your files ...

$path "/home/users/web/b75/ipw.yoursite/public_web/myvideos/".$file// the file made available for download via this PHP file
$mm_type="application/octet-stream"// modify accordingly to the file type of $path, but in most cases no need to do so

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");

readfile($path); // outputs the content of the file

exit();
}
else{
header ("location: http://www.yoursite.com");
}
?>



.

Wow, THANK YOU!

One thing though.. I see how you are able to assign a random code to connect the user to the file. This is a different from the code I use to connect the user to the purchase page, correct?
nockemout is offline   Reply With Quote
Old 03-17-2010, 12:45 AM   PM User | #9
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
The code can be anything you want it to be ...
It's just a way for the PHP script to match it to the "real filename".

You just want to make sure nobody else can figure out what it is by using brute force.

You may want to make it so the user's email AND the code must both match.
Using MySQL queries pretty much gives you endless possibilities.
Don't forget you can JOIN two MySQL tables together ... shopping cart and download script?
mlseim is offline   Reply With Quote
Old 03-17-2010, 12:53 AM   PM User | #10
nockemout
New Coder

 
Join Date: Dec 2008
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
nockemout is an unknown quantity at this point
That means I have some reading to do

Thanks!
nockemout is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:10 AM.


Advertisement
Log in to turn off these ads.