mercredi 6 janvier 2016

Generate very Uniquer number in PHP - CodeIgniter

I am developing a very big application where i want to create very very unique random numbers which will never ever match the previous records.Here is my code

function random_number($maxlength = 17) {
    $chary = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
                    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
    $return_str = "";
    for ( $x=0; $x<=$maxlength; $x++ ) {
        $return_str .= $chary[rand(0, count($chary))];
    }
    return $return_str;
}

Now it works fine but some time it give me error

Notice: Undefined offset: 62 in test.php on line 8

Where line 8 is

$return_str .= $chary[rand(0, count($chary))];

Can anyone fix this for me?

Secondly is there any function in CI for random numbers?




Aucun commentaire:

Enregistrer un commentaire