Jump to content

random 10-digit code


coolphpdude

Recommended Posts

<?php 
       $key_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
        $licence_length = 10 ;
        for($i=0;$i<$licence_length;$i++){
            $chr = $key_characters[rand(0,strlen($key_characters))];
            $number .= $chr ;
        }
?>

 

thats should do it  ;)

Link to comment
Share on other sites

both ways work a treat. I've gone with dingus's code because it combines numbers and both upper and lowercase code so gives me more chance of it being unique. I just need to figure out the code now to check database to see if it exists and if it does stick it in a while loop to generate a different code.

 

Thanks for your help guys, much appreciated.

Link to comment
Share on other sites

it may be somewhat unique (have a low collision rate), but it depends what its going to be used for. Let's say it's generated when you register, if I know roughly when you registered I could generate the possibilities...

 

And i've just noticed, your only using the first 10 chars of the sha, well then there's no guarantee that it'll be unique!!!

Link to comment
Share on other sites

yeah, I noticed that if your generating a bunch at once, it will be the same.... but if your only using it once in a while as I am it is fine.

 

I am not using it for registration though..... I use it to pass a transaction to a gift card company and they pass it back. So if they match, then the transaction is valid if not I kill the transaction.

 

So, not the best solution for all situations, but it works for some.

 

:)

Link to comment
Share on other sites

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.