Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy making random string

    to all,
    is there an in built function in php that cna generate a random string? example:

    rand() are for generating numbers right?

    so what is the function for generating a string?what i mean of string is like probably---> fsr45mdk43 or something like that.

    pls help..thanking u in advance.
    Warm Regards,
    Mivec

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I used to have quite a nice way of making a random string but I have forgotten it. I think it used rand though.

    This is how invisionboard generate a random password

    PHP Code:
        function make_password()
        {
            
    $pass "";
            
    $chars = array(
                
    "1","2","3","4","5","6","7","8","9","0",
                
    "a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J",
                
    "k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T",
                
    "u","U","v","V","w","W","x","X","y","Y","z","Z");
        
            
    $count count($chars) - 1;
        
            
    srand((double)microtime()*1000000);

            for(
    $i 0$i 8$i++)
            {
                
    $pass .= $chars[rand(0$count)];
            }
        
            return(
    $pass);
        } 
    you could obviously change the length and function name. Also, i think the (double) method is no longer used so, you could replace:

    PHP Code:

    srand
    ((double)microtime()*1000000)

    with:

    $microtime microtime()*1000000;
    settype($microtime'float');
    srand($microtime); 

  • #3
    eak
    eak is offline
    Regular Coder eak's Avatar
    Join Date
    Jun 2002
    Location
    Nashville, TN
    Posts
    354
    Thanks
    0
    Thanked 26 Times in 26 Posts
    this link may help you. ( http://www.phpnoise.com/tutorials/1/2 )
    on this site is uses $string = md5(rand(0,9999));
    I have tried it out and it works. you just have to use substr($string,start#,end#); to get the desired string length.
    eak | "Doing a good deed is like wetting your pants; every one can see the results, but only you can feel the warmth."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •