Jump to content

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 :)

Link to comment
https://forums.phpfreaks.com/topic/10526-generating-a-bunch-of-random-codenumbers/
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
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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.