Jump to content

Archived

This topic is now archived and is closed to further replies.

SharkBait

Generating a bunch of random codenumbers

Recommended Posts

Ok, lets see if I can explain this better....


What I am looking to do is figure out a way to create a random code made up of alphanumeric characters.

The reason: So customers can enter this unique, generate code into a website for something special ;)

I would then have to insert the randomly generated codes into a database so that I can check to see if its been used or not used etc.

The code should less than 10 characters.

I am just unsure how I should tackle this. I thought of a hash or sort, but can I make a hash for 10 characters or less?

Perhaps some sort of Algorithm so that when they enter their code I can check to see if it is valid, and then I can check to see if its already used (as in already in the database)

Kind of like how credticard seeds work.

Any thoughts/advice is grealy appreicated :)

Share this post


Link to post
Share on other sites
umm... you mean like the same thing a random password generator/reset script does, like this:

aX87b92h3

?

Share this post


Link to post
Share on other sites
I made a small function that will generate random passwords a while back. You can change the $rand string to any characters you want to use.
[code]
function createPass(){
    $num_letters = 10;
    
    $rand = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    
    for($i=0; $i<$num_letters; $i++){
        $pass .= substr($rand, rand(0, strlen($rand)-1), 1);
    }
    
    return $pass;
}
[/code]
hope this helps ya

Share this post


Link to post
Share on other sites
there are a lot of ways to do it. you can hash the current timestamp and then substring the first 10 characters.

also, the above example can be simplified with str_shuffle;
[code]$rand = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$pass = substr(str_shuffle($rand),0,10);[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.