Google

Sunday, June 28, 2009

Get Random Key

Useful function for getting a random key

 

private function _getRandomKey($md5=false){
    $char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    $key = '';
    $char_length = strlen($char)-1;

    for ($i = 0; $i != 32;  $i++){
        $random_pos = rand(0, $char_length);
        $key .= $char{$random_pos};
    }

    return $key = $md5 ? md5($key) : $key;
}

No comments: