regular expression time out

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jgentes
    New Member
    • Jul 2007
    • 32

    regular expression time out

    Hey, I am attempting to create a random password string, and I am getting a timeout error on the regular expression. its giving me errors on the checks. line 25/27/29/31

    [code=php]
    function gen_secure($ran d_len = false)
    {
    $config = array(
    0 => "EXDHKFRLACYZJS WMPVGBNUT",
    1 => "jhwzqbgkmadovr snxyftpicue",
    2 => "47358926",
    3 => "`~!@#$^&*( )_-+=\\}]{[:;?/><",
    );
    if($rand_len)
    $length = rand(MIN_LEN,MA X_LEN);
    else
    $length = 8;

    $pass = '';
    for($i=0; $i<$length; $i++)
    {
    $tring = str_shuffle($co nfig[rand(0,count($c onfig))]);
    $int_start = rand(0, strlen($tring))-1;
    //Adds the character to the end of the password
    $pass .= substr($tring, $int_start, 1);
    }

    do
    {
    if( preg_match("/[A-Z]+/",$pass) )
    // Picks a random character in the string, grabs random character from array, replaces that 1 character
    $pass = str_replace( substr($pass, rand(0,$length) ,1), substr($config[0],rand(0, strlen($config[0])-1), 1), $pass );
    if( preg_match("/[a-z]+/",$pass) )
    $pass = str_replace( substr($pass, rand(0,$length) ,1), substr($config[1],rand(0, strlen($config[1])-1), 1), $pass );
    if( preg_match("/[0-9]+/",$pass) )
    $pass = str_replace( substr($pass, rand(0,$length) ,1), substr($config[2],rand(0, strlen($config[2])-1), 1), $pass );
    if( preg_match("/[\W]+/",$pass) )
    $pass = str_replace( substr($pass, rand(0,$length) ,1), substr($config[3],rand(0, strlen($config[3])-1), 1), $pass );
    }
    while( !preg_match("/([A-Z]+[a-z]+[0-9]+){".$length." }/",$pass) );

    return $pass;
    }
    [/code]
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Sounds like your getting stuck in the while loop.
    Could you post the errors you are getting?

    Also if I may suggest an alternate solution...
    I think it's easiest to give an example code:
    [code=php]
    function MakePassword($l ength) {
    $charString = "ABCDEFGHIJKLMN OPQRSTUVXYZ1234 567890";
    $password = "";

    while(strlen($p assword) < $length) {
    $password .= $charString[rand(0, strlen($charStr ing) -1)];
    }
    return $password;
    }
    [/code]
    Last edited by Atli; Sep 29 '07, 01:56 AM. Reason: Added a suggestion

    Comment

    • jgentes
      New Member
      • Jul 2007
      • 32

      #3
      Originally posted by Atli
      Also if I may suggest an alternate solution...
      I think it's easiest to give an example code:
      The code I did provide is the example... If you want I will walk through it?

      the function is attempting to generate a password.
      the configure array contains all of the values that are possible and required at minimum

      1 UpperCase Letter, 1 Lowercase Letter, 1 Digit, 1 Symbol (non word character)

      It then checks if the user selects a random length.
      defaulted at 8 characters, but if true will pick between the minimum and maximum length constraints defined constants at the top of the page.

      it then creates the initial password, randomly selecting an array from the configure. shuffling the string to provide an extra layer of security.

      now the part I am getting errors on.
      It then checks the password for the requirements and if it does not have one of the requirements it randomly selects a position in the password string to, and replaces it with a random character from the appropriate configure string. and at the end it checks the string again to make sure it didnt over-write another password requirement.

      when all elements have been satisfied in accordance, it returns the password.


      the error is
      [28-Sep-2007 14:27:55] PHP Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/web/genPass.php on line 34


      ps in attempts to trouble shoot I have also tried printing out the password after it was first generated, and when the "check" loops is taken out the script works fine, however the password is not always compliant

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        I was talking about my alternative solution :)

        As I say, the problem is with your do while loop. The preg_match() function is apparently never able to validate your password, which causes it to loop forever... or until the 30 second limit is up and it returns the error.

        Again, let me offer an alternative to you approach. This is basically the same idea you had, but done differently.
        [code=php]
        <?php
        function MakePassword($l ength) {
        # Create data array
        $charArr = array(
        "upper" => "abcdefg",
        "lower" => "ABCDEFG",
        "digits" => "1234567890 ",
        "symbols" => "!$#?|"
        );

        # Create password
        $password = "";
        while(strlen($p assword) < $length) {
        foreach($charAr r as $chars) {
        $password .= $chars[rand(0, strlen($chars) - 1)];
        }
        }

        # Make sure password is not longer than $length
        if(strlen($pass word) > $length) {
        $password = substr($passwor d, 0, $length);
        }

        # Shuffle and return password
        return str_shuffle($pa ssword);
        }
        ?>
        [/code]
        Last edited by Atli; Oct 3 '07, 12:57 AM. Reason: Added suggestion

        Comment

        Working...