Ir para o conteúdo

Gerador de strings aleatórias

Esta função devolve uma string aleatória. Pode ser usada para gerar passwords ou identificadores únicos e aleatórios.

<?php

function create_random_string($length = 8) {
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; //caracteres possíveis
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i < $length) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
?>

Utilização

$random_string = create_random_string(8);
echo "String gerada: $random_string";