[snippet] Create Random Strings, PHP

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by Brandon, Jul 27, 2007.

  1. Brandon

    Brandon Former Senior Account Rep Community Support

    Messages:
    19,181
    Likes Received:
    28
    Trophy Points:
    48
    Ok, to create a random string in php use this great code snippet I made

    PHP:
    <?php

    // Brandon <brandon@phpriots.com>
    // Support: http://www.phpriots.com/forums/
    // Rev 1

    // Edit Below
    $min "5"// Minimum Password Length
    $max "15"// Maximun Password Length

    ################## Don't edit bellow unless you know what you are doing ##################

    // Letter/number Array
    $useable_characters array_merge(range('A','Z'),range('a','z'),range(0,9));

    // Make the length
    $length rand($min,$max);

    $password "";

    for (
    $i 0$i $length$i++)
    {
      
    $newrand rand(0,61);
      
    $password .= $useable_characters[$newrand];
    }

    ################## Echo's the password out, or do something else with it ##################

    echo $password;
    ?>
    Any support questions can be asked here or at the phpRiots forums.
     
    Last edited: Jul 27, 2007
  2. YamiKaitou

    YamiKaitou Member

    Messages:
    636
    Likes Received:
    0
    Trophy Points:
    16
    I have always hated typing out all of those letters/numbers.

    But what is the difference from that and using this? Anything?
    PHP:
    $useable_characters array_merge(range('A','Z'),range('a','z'),range(0,9));
    I use something similar to this with my captcha image
     
    Last edited: Jul 27, 2007
  3. Brandon

    Brandon Former Senior Account Rep Community Support

    Messages:
    19,181
    Likes Received:
    28
    Trophy Points:
    48
    The fact that I never knew about that:drool:

    That does seem more handy though, mind if I use it?
     
  4. YamiKaitou

    YamiKaitou Member

    Messages:
    636
    Likes Received:
    0
    Trophy Points:
    16
    Not at all, I found out about it from someplace else. Can't remember where
     
  5. Brandon

    Brandon Former Senior Account Rep Community Support

    Messages:
    19,181
    Likes Received:
    28
    Trophy Points:
    48
    Thanks, I updated the initial post to include that, seems to work so thanks.
     

Share This Page