Jump to content

Recommended Posts

<?php

echo rand();

?>


 

This generates a random number, and to control from what minimum number to maximum, you have to enter min, and max values like this:

 

<?php

echo rand(1,10);

?>

 

This generates random number from 1-10.

 

Im not to sure how you can get letters in there though...

 

 

Write a function to do it based on the criteria/format you're looking for and then check the serial generated in your database to make sure it's not already used (if so, generated a different one and check again).

 

You can use the char(rand(65, 90)) to produce a random character between A to Z.

 

http://us.php.net/char

 

 

if u are going to store that product serial number in a database please do not use it as the PK of the table but as a secondary item to the database it will help you alot.

 

Also if u do do what i said make sure u check each serial number against the database before assuming its the valid because 1 in a billion (probably a trillion) will match but if u have a million+ rows in a database that will improve a chance of a match

$str = array();
for($i = 0; $i < 20; $i++){
      if($letter_OR_number = rand(0,1)){ // true: alphabet chosen
         $str[] = chr(rand(65, 90));
      } else { // false: number chosen
         $str[] = rand(0,9);
      }
      if($i % 4 == 3){
         $str[] = ' ';
      }
}

foreach($str as $val){
echo $val;
}

 

The above code doesn't insert a '-' inbetween every 4 characters.. but if you need it, substitute the modulus if statement with:

if($i % 4 == 3){
   ($i < 19)? $str[] = '-': $str[] = ' ';
}

or

<?php
function sernum()
{
    $template   = 'XX99-XX99-99XX-99XX-XXXX-99XX';
    $k = strlen($template);
    $sernum = '';
    for ($i=0; $i<$k; $i++)
    {
        switch($template[$i])
        {
            case 'X': $sernum .= chr(rand(65,90)); break;
            case '9': $sernum .= rand(0,9); break;
            case '-': $sernum .= '-';  break; 
        }
    }
    return $sernum;
}

    /**
    * try it
    */
echo '<pre>';

for ($i=0; $i < 10; $i++) echo sernum(), '<br/>';

echo '</pre>';
?>    

-->
WS41-IZ91-55XO-23WA-WVZS-20VK
SJ42-CV50-79DA-55UV-TERR-28IJ
LY80-CN84-69LV-73EW-ZZEU-09AI
IS86-RG15-39CG-38HK-XLUG-86FO
QX64-ZG67-31IO-45IS-SVSJ-91BG
OU45-IW50-67KZ-24PZ-QLGI-56XR
KP73-YG29-50PT-19IN-ALPK-94ZD
JP59-CR85-42CE-32UN-ZDBV-38MU
TA16-AL61-46EL-95EH-XTVW-00SZ
UC87-YS99-84CV-42UA-YOFV-23WX

 

 

This is another way but it the long way around the format not the same......

 

learnig the last post

 

 

<?php
$x=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
$y=array("0","1","2","3","4","5","6","7","8","9");

shuffle($x);
shuffle($y);

$x=implode('',$x);
$y=implode('',$y);

$x=strtoupper($x);

$res1=substr($x,0,2);
$res2=substr($y,1,2);

$res3=substr($x,2,3);
$res4=substr($y,0,1);

$res5=substr($x,0,3);
$res6=substr($y,0,2);

$result=$res1.$res2;
$result1=$res3.$res4;
$result2=$res5.$res6;

echo "$result-$result1-$result2";

?>

  • 3 years later...

Hi, Barand! ..

 

I'm php newbie n I really like your php key generator..

 

but Now I don't know how to add your generated key to mysql database..

 

Definitely, I'm trying to change WoW default Keygenerator to xxxx-xxxx-xxxx-xxxx-xxxx format.

 

function createKeys()

{
global $Account, $DB;
    if($_POST['num'] < 300)
{


        $keys_arr = $Account->generate_keys($_POST['num']);
        foreach ($keys_arr as $key) 
	{	

            $DB->query("INSERT INTO mw_regkeys (`key`) VALUES('$key')");
        }
    }

    output_message('success', $_POST['num'].' Keys added to the database! Please wait to be re-directed...
	<meta http-equiv=refresh content="3;url=?p=admin&sub=regkeys">');
}

It's in create function. I don't know where and how I've to use it to change.

 

Is there anyone can guide me to change?

 

 

or

<?php
function sernum()
{
    $template   = 'XX99-XX99-99XX-99XX-XXXX-99XX';
    $k = strlen($template);
    $sernum = '';
    for ($i=0; $i<$k; $i++)
    {
        switch($template[$i])
        {
            case 'X': $sernum .= chr(rand(65,90)); break;
            case '9': $sernum .= rand(0,9); break;
            case '-': $sernum .= '-';  break; 
        }
    }
    return $sernum;
}

    /**
    * try it
    */
echo '<pre>';

for ($i=0; $i < 10; $i++) echo sernum(), '<br/>';

echo '</pre>';
?>    

-->
WS41-IZ91-55XO-23WA-WVZS-20VK
SJ42-CV50-79DA-55UV-TERR-28IJ
LY80-CN84-69LV-73EW-ZZEU-09AI
IS86-RG15-39CG-38HK-XLUG-86FO
QX64-ZG67-31IO-45IS-SVSJ-91BG
OU45-IW50-67KZ-24PZ-QLGI-56XR
KP73-YG29-50PT-19IN-ALPK-94ZD
JP59-CR85-42CE-32UN-ZDBV-38MU
TA16-AL61-46EL-95EH-XTVW-00SZ
UC87-YS99-84CV-42UA-YOFV-23WX

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.