how to genrate random string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

how to genrate random string

Post by itsmani1 »

hi there...
i want to genrate a random string like a3nd29sas3 can any one help me.
thanks.
method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

Post by method_man »

ive searched this site and found some different solutions.

1.

Code: Select all

<?php
$string = password();//a function which returns 5 letters/numbers/mixed value
$check = mysql_num_rows(mysql_query("select password from table where password = '$string'"));
if($check > 0)
{
$rand = rand(10, 32);
$md = md5($string.time());
$string .= $md{$rand}.($check + 1);
}
?>
2.

Code: Select all

<?php
 
function makeRand ($minlength, $maxlength, $useupper, $usespecial, $usenumbers) 
  { //Random string generator
    $key="";
    $charset = "abcdefghijklmnopqrstuvwxyz";        
    if ($useupper)   $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    if ($usenumbers) $charset .= "0123456789"; 
    if ($usespecial) $charset .= "~@#$%^*()_+-={}|][";   
    if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength); 
    else                         $length = mt_rand ($minlength, $maxlength); 
    for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; 
    return $key; 
  }
 
      do
      {
        $tempPass="";
        $tempPass=$this->utils->makeRand(15,15,1,0,1);        
      } while(in_array($tempPass,$arrPass));
 
//$arrPass is a array of present passwords
 
?>
and 3.

Code: Select all

require_once 'Text/Password.php'; //see http://pear.php.net/package/Text_Password
$password = Text_Password::create(8);
all 3 of these can be found on this url: viewtopic.php?t=22563&highlight=random+string
method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

Post by method_man »

i think thats what your talking about
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

okies thanks let me try
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

anjanesh wrote this little random text generator too...

(Conveniently named simpleRandomTextGenerater :P)

viewtopic.php?t=34337
Post Reply