Jump to content

function loops


oracle259

Recommended Posts

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

I think this will work, but I haven't tested so I am not sure-

[code]<?php

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);
  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

Try this maybe:

[code]<?php

if (!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

Thanks it works

[code]
<?php

if (!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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.