Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating a random code in php?

Tags:

php

random

i know this might seem silly, but i want to generate a random code of 8 characetrs, only numbers or letters using php. i needs this to generate a password for each user that signs up, thanks

like image 802
getaway Avatar asked Dec 31 '10 15:12

getaway


2 Answers

I would rather use md5 to generate passwords

But you can use something like this if you want a custom:

function createRandomPassword() { 

    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
    srand((double)microtime()*1000000); 
    $i = 0; 
    $pass = '' ; 

    while ($i <= 7) { 
        $num = rand() % 33; 
        $tmp = substr($chars, $num, 1); 
        $pass = $pass . $tmp; 
        $i++; 
    } 

    return $pass; 

} 
like image 71
eriksv88 Avatar answered Oct 01 '22 21:10

eriksv88


What about something like this, for ease:

$pass = substr(md5(uniqid(mt_rand(), true)) , 0, 8);
like image 20
Jeremy Avatar answered Oct 01 '22 20:10

Jeremy