random1 Posted August 17, 2008 Share Posted August 17, 2008 How can you use PHP to generate a serial number? e.g. DF34-VB89-98AP-34NQ-QQUI-23AW Quote Link to comment https://forums.phpfreaks.com/topic/120028-solved-how-to-generate-a-product-serial-number/ Share on other sites More sharing options...
gs Posted August 17, 2008 Share Posted August 17, 2008 <?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... Quote Link to comment https://forums.phpfreaks.com/topic/120028-solved-how-to-generate-a-product-serial-number/#findComment-618323 Share on other sites More sharing options...
toplay Posted August 17, 2008 Share Posted August 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/120028-solved-how-to-generate-a-product-serial-number/#findComment-618328 Share on other sites More sharing options...
cooldude832 Posted August 17, 2008 Share Posted August 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/120028-solved-how-to-generate-a-product-serial-number/#findComment-618347 Share on other sites More sharing options...
nrg_alpha Posted August 17, 2008 Share Posted August 17, 2008 $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[] = ' '; } Quote Link to comment https://forums.phpfreaks.com/topic/120028-solved-how-to-generate-a-product-serial-number/#findComment-618449 Share on other sites More sharing options...
Barand Posted August 17, 2008 Share Posted August 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/120028-solved-how-to-generate-a-product-serial-number/#findComment-618471 Share on other sites More sharing options...
random1 Posted August 18, 2008 Author Share Posted August 18, 2008 Thanks everyone, very helpful Props to Barand for creating a really good example. Quote Link to comment https://forums.phpfreaks.com/topic/120028-solved-how-to-generate-a-product-serial-number/#findComment-618819 Share on other sites More sharing options...
redarrow Posted August 18, 2008 Share Posted August 18, 2008 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/120028-solved-how-to-generate-a-product-serial-number/#findComment-618832 Share on other sites More sharing options...
mr_michael Posted August 26, 2011 Share Posted August 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/120028-solved-how-to-generate-a-product-serial-number/#findComment-1262169 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.