You are here:Home » PHP » Random String Generation

Random String Generation


Just call the function with the parameter being the length of string you want to generate
function generatestring($length)
{
    $key="";
    $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for($i=0; $i<$length; $i++) 
    {
    $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; 
    }
    return $key;
}