473,385 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

regular expression time out

32
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

Expand|Select|Wrap|Line Numbers
  1. function gen_secure($rand_len = false)
  2. {
  3.     $config = array(
  4.         0 => "EXDHKFRLACYZJSWMPVGBNUT",
  5.         1 => "jhwzqbgkmadovrsnxyftpicue",
  6.         2 => "47358926",
  7.         3 => "`~!@#$^&*()_-+=\\}]{[:;?/><",
  8.     );
  9.     if($rand_len)
  10.         $length = rand(MIN_LEN,MAX_LEN);
  11.     else 
  12.         $length = 8;
  13.  
  14.     $pass = '';
  15.     for($i=0; $i<$length; $i++)
  16.     {
  17.         $tring = str_shuffle($config[rand(0,count($config))]);
  18.         $int_start = rand(0, strlen($tring))-1;
  19.         //Adds the character to the end of the password
  20.         $pass .= substr($tring, $int_start, 1);
  21.     }
  22.  
  23.     do
  24.     {
  25.         if( preg_match("/[A-Z]+/",$pass) )
  26.             // Picks a random character in the string, grabs random character from array, replaces that 1 character
  27.             $pass = str_replace( substr($pass, rand(0,$length),1), substr($config[0],rand(0, strlen($config[0])-1), 1), $pass );
  28.         if( preg_match("/[a-z]+/",$pass) )
  29.             $pass = str_replace( substr($pass, rand(0,$length),1), substr($config[1],rand(0, strlen($config[1])-1), 1), $pass );
  30.         if( preg_match("/[0-9]+/",$pass) )
  31.             $pass = str_replace( substr($pass, rand(0,$length),1), substr($config[2],rand(0, strlen($config[2])-1), 1), $pass );
  32.         if( preg_match("/[\W]+/",$pass) )
  33.             $pass = str_replace( substr($pass, rand(0,$length),1), substr($config[3],rand(0, strlen($config[3])-1), 1), $pass );
  34.     }
  35.     while( !preg_match("/([A-Z]+[a-z]+[0-9]+){".$length."}/",$pass) );
  36.  
  37.     return $pass;
  38. }
  39.  
Sep 28 '07 #1
3 2661
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. function MakePassword($length) {
  2.   $charString = "ABCDEFGHIJKLMNOPQRSTUVXYZ1234567890";
  3.   $password = "";
  4.  
  5.   while(strlen($password) < $length) {
  6.     $password .= $charString[rand(0, strlen($charString) -1)];
  7.   }
  8.   return $password;
  9. }
  10.  
Sep 29 '07 #2
jgentes
32
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
Oct 2 '07 #3
Atli
5,058 Expert 4TB
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function MakePassword($length) {
  3.     # Create data array
  4.     $charArr = array(
  5.       "upper" => "abcdefg",
  6.       "lower" => "ABCDEFG",
  7.       "digits" => "1234567890",
  8.       "symbols" => "!$#?|"
  9.     );
  10.  
  11.     # Create password
  12.     $password = "";
  13.     while(strlen($password) < $length) {
  14.         foreach($charArr as $chars) {
  15.             $password .= $chars[rand(0, strlen($chars) - 1)];
  16.         }
  17.     }
  18.  
  19.     # Make sure password is not longer than $length
  20.     if(strlen($password) > $length) {
  21.         $password = substr($password, 0, $length);
  22.     }
  23.  
  24.     # Shuffle and return password
  25.     return str_shuffle($password);
  26. }
  27. ?>
  28.  
Oct 3 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
2
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text...
15
by: Mark Rae | last post by:
Hi, I'm trying to construct a RegEx pattern which will validate a string so that it can contain: only the numerical characters from 0 to 9 i.e. no decimal points, negative signs, exponentials...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.