Tuesday, June 26, 2012

php: generates Token with random string

The following code generate the random string with certain length to the client. It can be used to create the token and send to client's apps.

<?php
    
    // generate random string
    function random_gen($length)
    {
        $random= "";
        srand((double)microtime()*1000000);
        $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $char_list .= "abcdefghijklmnopqrstuvwxyz";
        $char_list .= "1234567890";
        // Add the special characters to $char_list if needed
        
        for($i = 0; $i < $length; $i++)  
        {    
            $random .= substr($char_list,(rand()%(strlen($char_list))), 1);  
        }  
        return $random;
    } 
    
    $random_string = random_gen(30); //This will return a random 30 character string
    echo "New Token: ".$random_string;
    
   
?>

No comments:

Post a Comment