oracle259 Posted November 1, 2006 Share Posted November 1, 2006 I have the following code that checks to see if $hash is a particular pattern if it is ok, if not it regenerates the $hash. But how do i modify the code so as to validate the regenerated hash againts the validation pattern.I think it will require some loop but i cant seem to crack it[code] if (!isset($hash) || empty($hash)) { regenerateHash(); } else { validateHash($hash); }function validateHash($hash) { global $hash; $pattern = "/^(?=.*?[0-9])(?=.*?[a-zA-Z])[a-zA-Z0-9]*$/i"; $is_pattern = preg_match($pattern, $hash); if (!$is_pattern) { $hash = regenerateHash(); } else { return $hash; } } function regenerateHash() { srand((double)microtime()*1000000); $new_hash = sha1(trim(uniqid(rand(), 80))); return $new_hash; }[/code]thanks in advance Link to comment https://forums.phpfreaks.com/topic/25821-function-loops/ Share on other sites More sharing options...
Orio Posted November 1, 2006 Share Posted November 1, 2006 I think this will work, but I haven't tested so I am not sure-[code]<?phpif (!isset($hash) || empty($hash)) { regenerateHash(); } else { validateHash($hash); }function validateHash($hash){ global $hash; $pattern = "/^(?=.*?[0-9])(?=.*?[a-zA-Z])[a-zA-Z0-9]*$/i"; $is_pattern = preg_match($pattern, $hash); while(!is_pattern) { $hash = regenerateHash(); $is_pattern = preg_match($pattern, $hash); } return $hash;} function regenerateHash(){ srand((double)microtime()*1000000); $new_hash = sha1(trim(uniqid(rand(), 80))); return validateHash($new_hash);}?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/25821-function-loops/#findComment-117925 Share on other sites More sharing options...
oracle259 Posted November 1, 2006 Author Share Posted November 1, 2006 Sorry it didnt work Link to comment https://forums.phpfreaks.com/topic/25821-function-loops/#findComment-117934 Share on other sites More sharing options...
Orio Posted November 1, 2006 Share Posted November 1, 2006 Try this maybe:[code]<?phpif (!isset($hash) || empty($hash)) regenerateHash();echo validateHash($hash);function validateHash($hash){ global $hash; $pattern = "/^(?=.*?[0-9])(?=.*?[a-zA-Z])[a-zA-Z0-9]*$/i"; $is_pattern = preg_match($pattern, $hash); while(!is_pattern) { $hash = regenerateHash(); $is_pattern = preg_match($pattern, $hash); } return $hash;} function regenerateHash(){ srand((double)microtime()*1000000); $new_hash = sha1(trim(uniqid(rand(), 80))); return $new_hash;}?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/25821-function-loops/#findComment-117977 Share on other sites More sharing options...
oracle259 Posted November 1, 2006 Author Share Posted November 1, 2006 Thanks it works[code]<?phpif (!isset($hash) || empty($hash)) regenerateHash();echo validateHash($hash);function validateHash($hash){ global $hash; $pattern = "/^(?=.*?[0-9])(?=.*?[a-zA-Z])[a-zA-Z0-9]*$/i"; $is_pattern = preg_match($pattern, $hash); while(!$is_pattern) { $hash = regenerateHash(); $is_pattern = preg_match($pattern, $hash); } return $hash;} function regenerateHash(){ srand((double)microtime()*1000000); $new_hash = sha1(trim(uniqid(rand(), 80))); return $new_hash;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/25821-function-loops/#findComment-117997 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.