Zend - The PHP Company




Passwords

Add Code


Random ASCII key generator (length variable)  

Type: code fragment
Added by: Kawa
Entered: 05/07/2002
Last modified: 09/12/2009
Rating: - (fewer than 3 votes)
Views: 6537
Function to get a random ASCII Key, usefull to get a Primary Key, or get a password,... The length can be passed as argument


<?php
function getRand(&$str,$max=10) {

        
srand((double) microtime() * 1000000);
        
$randval=rand(48,122);

        while(
strlen($str)<$max) {
                if(
                        (
$randval>47 AND $randval<58) OR
                        (
$randval>64 AND $randval<91) OR
                        (
$randval>96 AND $randval<123)
                ) {
                        
$str.=chr($randval);
                        
getRand(&$str,&$max);
                }
                else {
                        
getRand(&$str,&$max);
                }
        }
}
getRand($str,$len);
print 
"<LI>".$len;
print 
"<LI>".$str;
?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories