Ver Mensaje Individual
  #8 (permalink)  
Antiguo 12/08/2012, 00:24
israel965
 
Fecha de Ingreso: mayo-2012
Mensajes: 49
Antigüedad: 12 años, 10 meses
Puntos: 2
Respuesta: numero y letra aleatorio con php

Cita:
Iniciado por delrdlp Ver Mensaje
Acá te paso una función para generar claves aleatorias de x dígitos:

Código PHP:
function randomText($length) {

    
$pattern "1234567890abcdefghijklmnopqrstuvwxyz";

    for(
$i 0$i $length$i++) {

        
$key .= $pattern{rand(035)};

    }

    return 
$key;


la llamarías con $clave = randomText(8);

Luego que la tengas generada, tendrás que hacer una consulta en un bucle:

Código PHP:
$existe true;
while(
$existe) {

<
EJECUTAS CONSULTA"select count(*) from xxx where hash = '$clave'"
 
if(<EXISTE LA CLAVE>) {
  
$clave randomText(8);
 } else {
  
$existe false;
 }


Por si viene alguién que recién empieza y no le sirve, que sustituya key por lenght
en la función, que es lo que tenemos que recuperar, perdón por revivir un tema tan viejo.