Posted By

bgriffiths on 05/19/09


Tagged

code key unique


Versions (?)

Create Unique Code Replacing Lookalike Characters


 / Published in: PHP
 

  1. function create_key(&$cnnDB){
  2. $letters = range('A','Z');
  3. $numbers = range(0,9);
  4. $all = array_merge($numbers,$letters);
  5. $foundkey = false;
  6. while(!$foundkey){
  7. for($i=0;$i<5;$i++){
  8. $num = rand(0,count($all)-1);
  9. $key .= $all[$num];
  10. $total += $num;
  11. }
  12.  
  13. //Replace Oh with Zero, I with one
  14. $key = str_replace(
  15. array('O','I'),
  16. array('0','1'),
  17. $key
  18. );
  19.  
  20. //make checksum
  21. $key .= $all[($total % count($all))];
  22.  
  23. $key = str_replace(
  24. array('O','I'),
  25. array('0','1'),
  26. $key
  27. );
  28.  
  29. $SQL = 'select C_Key from customers where C_Key = "'.$key.'"';
  30. $result = $cnnDB->GetRow($SQL);
  31. if(count($result) == 0){
  32. $foundkey = true;
  33. }
  34. }
  35.  
  36. return $key;
  37. }

Report this snippet  

You need to login to post a comment.