Ulyssesonline

the tech surfer

  • Home
  • About
  • Archives
You are here: Home / General / Generating PHP Random Keys

June 4, 2010

Generating PHP Random Keys

There are many ways of generating random keys in PHP. Random keys are essential in applications where you need unique identifiers that are not necessarily in sequential order. A prime example of where to use this script would be is when you need to create unique key for a URL shortener script.

For example: a user supplies a long URL. The script generates a random key based a predetermined length and returns a short URL based on the random key. So, here are  a couple of PHP random scripts that you might find useful. They both generate 6 random characters each time they are called.

Example 1

We create a function called rand_str(). We set the characters and generate a random key with a length of 6 characters. We make sure the same two characters don’t appear next to each other.

function rand_str($length = 6, $chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890?) {
  $chars_length = (strlen($chars) – 1); // Length of character list
  $string = $chars{rand(0, $chars_length)}; // Start our string
  for ($i = 1; $i < $length; $i = strlen($string)) { // Generate random string
    $r = $chars{rand(0, $chars_length)}; // Grab a random character from our list
    if ($r != $string{$i – 1}) { 
      $string .=  $r; // Make sure the same two characters don’t appear next to each other
    } 
    echo $string; // Return the string
  }
}

function rand_str($length = 6, $chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890?) { $chars_length = (strlen($chars) – 1); // Length of character list $string = $chars{rand(0, $chars_length)}; // Start our string for ($i = 1; $i < $length; $i = strlen($string)) { // Generate random string $r = $chars{rand(0, $chars_length)}; // Grab a random character from our list if ($r != $string{$i – 1}) { $string .= $r; // Make sure the same two characters don’t appear next to each other } echo $string; // Return the string } }

Example 2

This is example is essentially the same as above, but a bit more succinct. It generates a 6 character random string, but it doesn’t have a consecutive character feature like above.

for ($s = ”, $i = 0, $z = strlen($a = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’)-1; $i != 6; $x = rand(0,$z), $s .= $a{$x}, $i++);
$key = $s;
echo $key;

for ($s = ”, $i = 0, $z = strlen($a = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’)-1; $i != 6; $x = rand(0,$z), $s .= $a{$x}, $i++); $key = $s; echo $key;

There you have it. Two scripts for generating random strings based on a predetermined length. It’s perfect for applications that need to generate unique and random keys.

There’s a good possibility a “swear” word could be generated by random chance. To avoid such words, simply take out the vowels from your character list.

Filed Under: General, PHP Tagged With: PHP, random, scrip, url shortener

Copyright © 2003 - 2018