Jump to content

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
https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587598
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
https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587629
Share on other sites

mbeals rand is limited to 32768

 

Note: On some platforms (such as Windows) RAND_MAX is only 32768. If you require a range larger than 32768, specifying min and max will allow you to create a range larger than RAND_MAX, or consider using mt_rand() instead.

Link to comment
https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588283
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
https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588462
Share on other sites

Exactly, not what you'd call random

 

<?php
$a = array();
for ($i=0; $i<1000; $i++)  $a[] = substr(sha1(time()),0,10);

$ka = array_count_values($a);
echo '<pre>', print_r($ka, true), '</pre>';
?>

1000 generated and all the same :(

Link to comment
https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588466
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
https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588484
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.