Created
February 27, 2016 18:24
-
-
Save cuonggt/b39ac7b513d518789e68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function generateCode($id) { | |
| $codeStr = (1000000000 + $id) . generateRandomSerialNumber(4); | |
| return $codeStr . generateCheckDigit($codeStr); | |
| } | |
| function generateRandomSerialNumber($length) | |
| { | |
| $vowels = '0123456789'; | |
| $consonants = '0123456789'; | |
| $password = ''; | |
| $alt = time() % 2; | |
| for ($i = 0; $i < $length; $i++) | |
| { | |
| if ($alt == 1) | |
| { | |
| $password .= $consonants[(rand() % strlen($consonants))]; | |
| $alt = 0; | |
| } | |
| else | |
| { | |
| $password .= $vowels[(rand() % strlen($vowels))]; | |
| $alt = 1; | |
| } | |
| } | |
| return $password; | |
| } | |
| function generateCheckDigit($codeStr) { | |
| $odd_total = 0; | |
| $even_total = 0; | |
| $codeLength = strlen($codeStr); | |
| for ($i =0; $i < $codeLength; $i++) | |
| { | |
| if ((($i+1)%2) == 0) { | |
| $even_total += $codeStr[$i]; | |
| } else { | |
| $odd_total += $codeStr[$i]; | |
| } | |
| } | |
| $sum = (3 * $odd_total) + $even_total; | |
| $check_digit = $sum % 10; | |
| return ($check_digit > 0) ? 10 - $check_digit : $check_digit; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment