[PHP] Loop until false

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hello,

So basically I'm trying to identify users other than using their IP addresses, so I wanted to give them random strings which would each be unique.

With this in mind, I need to figure a way of re-generating a string each time the same one already exists in the database... something like in the diagram below:

logic.png


So basically I got up to the part where I check if the string exists in the database then I return true if it does and false if it does not, so I need to loop until said variable returns false.

How can I do this?
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
So here's what I currently have for generating a new identity.

Code:
public function createIdentity()
        {
            global $con;
            function getIdentity($identity)
            {
                global $con;
                $q = "SELECT * FROM identities WHERE identity = '{$identity}'";
                if(!$result = $con->query($q))
                {
                    die("Could not execute query.");
                }
                $count = $result->num_rows;
                if($count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            $check = getIdentity($this->generateIdentity());
        }
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
I could do that, but doing it this way means I'll also learn something new.
I mean, there's nothing wrong with ID's... but I need each identity to be exactly 16 characters.
 

Divide

Member
Jul 13, 2013
45
11
Code:
function randomstring($length = 10)
{
   $characters = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
   $randstring = '';
   for ($i = 0; $i < $length; $i++)
   {
      $randstring = $characters[rand(0, strlen($characters))];
   }
   return $randstring;
}
 
function indatabase($string)
{
   global $con;
   $q = "SELECT * FROM identities WHERE identity = '{$string}'";
   if(!$result = $con->query($q))
   {
      die("Could not execute query.");
   }
   $count = $result->num_rows;
   if($count > 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}
 
 
public function createIdentity()
{
   $found = false;
   $checkstring = "";
   $finalstring = "";
   $times = 0;
   while(!$found)
   {
      if($times > 20)
      {
         $found = true;
         die("Attempted to find a key too many times!");
      }
      $times++;
      $checkstring = randomstring();
      if(!indatabase($checkstring))
      {
         $found = true;
         $finalstring = $checkstring;
      }
   }
   $return $finalstring;
}

Will return a string which isn't already in the database, and will only check 20 times.

Took about 20 mins of my life to code. Will come in useful for me too :)
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,398
962
Code:
function randomstring($length = 10)
{
  $characters = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
  $randstring = '';
  for ($i = 0; $i < $length; $i++)
  {
      $randstring = $characters[rand(0, strlen($characters))];
  }
  return $randstring;
}

Took about 20 mins of my life to code. Will come in useful for me too :)


I can see why it took 20 minutes:

PHP:
$randstring = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 10);
 

Users who are viewing this thread

Top