limiting confirmation code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

limiting confirmation code

Post by adsegzy »

Hello My Success-Oriented Friends,

Is it possible to limit the generated confirmation code to a specific limit, that is may be to general a 20 digits confirmation code.

Regards,
adsegzy
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: limiting confirmation code

Post by Weiry »

I really hate to ask this... but,

What generated confirmation code?
No code? No reference? No hint?

Im sure when i did a php.net search for a confirmation function it didnt return one :?
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

Re: limiting confirmation code

Post by adsegzy »

Hello Weiry,

Is it that you dont know how to generate confirmation code? let me know so that i can post the code for genenrating confirmation code.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: limiting confirmation code

Post by superdezign »

Create a whitelist of characters that can be in the confirmation code, and randomly add them to the code.

Code: Select all

function generateConfirmationCode($length = 30) {
  $whitelist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $confirmationCode = "";
 
  for ($i = 0; $i < $length; $i++) {
    $confirmationCode .= $whitelist[rand(0, strlen($whitelist) - 1)];
  }
}
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: limiting confirmation code

Post by Weiry »

adsegzy wrote:s it possible to limit the generated confirmation code to a specific limit,
If you state your questions more carefully, i can answer better. You ask is it possible, but you make no reference to that you are actually asking for a confirmation code script. So if you were using a script you already had, i wouldnt know, but if you made one yourself, then yes it is possible.

superdezign posted one way of doing it, and here is mine:

Code: Select all

    function getConfirmation($length){
        $returnArray = array();
        for($i = 0; $i<$length; $i++){
            array_push($returnArray,chr(mt_rand(63,126)));
        }
        return implode($returnArray);
    }
    print getConfirmation(20);
With this you don't need to manually set the characters your confirmation code uses. This is calculated from chr().
Normally i would set the mt_rand() between 33,126 but i was experiencing a problem with a few of the symbol ascii characters.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: limiting confirmation code

Post by Mirge »

Another way to do it: http://us2.php.net/uniqid/

I've used this a few times :)
Post Reply