4

I'm trying to use PHP as a way to process this in a webpage, my typical language is java so i am unfamiliar with how this would be done for product keys.

This is basically the process:

1. Generate random string with format XX-XXXX-XXXX-XXX a mix of numbers and letters.
2. Check if it already exists in an SQL database
3. If exists, generate another one and repeat?

So how would I do this using PHP? Please explain what i would need to do and what is the best way of going about it.

  • If the generated key is going to be inserted once generated, I'd recommend placing a unique constraint on the column. You can then test if the key is already in the database by seeing if an error occurs when inserting the key. As for generating a random string, there are [tons](http://stackoverflow.com/questions/4356289/php-random-string-generator) [of](http://stackoverflow.com/questions/6101956/generating-a-random-password-in-php) [examples](http://stackoverflow.com/questions/19017694/1line-php-random-string-generator). Do you want the Xs to only be from a specific character set? – Dave Chen Jun 26 '14 at 07:09
  • @Dave Chen Thank you, just the type of insight i was looking for! Yes A-Z and 0-9. Think of it as a product activation key. – user2816960 Jun 26 '14 at 07:10

3 Answers3

4

Generate random string from following function.

<?php
function randomString() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    $array_lengths = array(2,4,4,3);
    foreach($array_lengths as $v){
      for ($i = 0; $i < $v; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
      }
      $pass[] = '-';
    }
    return rtrim(implode($pass),'-'); //turn the array into a string
}

echo randomString();

?>

SQL Please create unique key field and use ON DUPLICATE KEY query for insert/ update data

DEMO

Girish
  • 11,907
  • 3
  • 34
  • 51
  • interesting how you used an array to set the lengths per section! using dave chen's comment on sql constaint and this string generation i think i can piece this together nicely. thank you, will mark as answer when SO lets me. – user2816960 Jun 26 '14 at 07:17
  • and a demo! is it my birthday? – user2816960 Jun 26 '14 at 07:18
2

You can generates randomnumbers key using this way.

echo rk(2)."-".rk(4)."-".rk(4)."-".rk(3);
function rk($chars) {
   $letters = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
   return substr(str_shuffle($letters), 0, $chars);
}
Rahul K
  • 413
  • 3
  • 11
1

Here's a process that could be of use!

<?php
/** The function below was taken from http://stackoverflow.com/questions/853813/how-to-create-a-random-string-using-php **/
function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'){
    $str = '';
    $count = strlen($charset);
    while ($length--) {
        $str .= $charset[mt_rand(0, $count-1)];
    }
    return $str;
}
/** The function that will do your work is: **/
function theProcess(){
/** Now to inefficiently concatenate your random string together. **/
    $theString = randString(2)."-".randString(4)."-".randString(4)."-".randString(3);
    echo $theString;
/** Proceed to query your database using MySQLi or PDO, with a query similar to: **/
    /** Add your preliminary (connection) code here. **/
    $sthandler = $dbhandler->prepare('SELECT 1 FROM products WHERE productKey = ?');
    $sthandler->execute(array($theString));
/** Check whether a result is returned from MySQL. **/
    if ($sthandler->rowCount() > 0) {
        exit("IT EXISTS!");
    } else {
        theProcess();
    }
}
/** Call the function the first time. **/
theProcess();
?>
TribalChief
  • 797
  • 5
  • 11