Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $name = $_GET['name'];
- $gender = $_GET['gender'];
- $pin = $_GET['pin']; // 9912164756
- $birthDate = substr($pin, 0, 6); // 991216
- $birthYear = substr($birthDate, 0, 2); // 99
- $birthMonth = substr($birthDate, 2, 2); // 12
- $birthDay = substr($birthDate, 4, 2); // 16
- $regionID = substr($pin, 6, 3); // 475
- $genderID = substr($regionID, 2, 1); // 5
- $checksum = substr($pin, 9, 1); // 6
- $checksumDigits = str_split(substr($pin, 0, 9));
- if (isValidName($name) && isValidGender($gender, $genderID) &&
- isValidMonth($birthMonth) && isValidDay($birthDay) &&
- isValidCheckSum($checksumDigits, $checksum) && isValidPinLength($pin)):
- $result = new stdClass();
- $result->name = $name;
- $result->gender = $gender;
- $result->pin = $pin;
- echo json_encode($result);
- else:
- echo "<h2>Incorrect data</h2>";
- endif;
- function isValidPinLength($pin) {
- $pinLength = strlen($pin);
- if ($pinLength === 10):
- return TRUE;
- else:
- return FALSE;
- endif;
- }
- function isValidName($name) {
- $nameArr = preg_split('/\s+/', $name);
- if (sizeof($nameArr) === 2):
- return TRUE;
- else:
- return FALSE;
- endif;
- }
- function isValidMonth($month) {
- if (($month >= 1 && $month <= 12) ||
- ($month >= 21 && $month <= 32) ||
- ($month >= 41 && $month <= 52)):
- return TRUE;
- else:
- return FALSE;
- endif;
- }
- function isValidDay($day) {
- if ($day >= 1 && $day <= 31):
- return TRUE;
- else:
- return FALSE;
- endif;
- }
- function isValidGender($gender, $genderID) {
- if (($gender === 'female' && $genderID % 2 === 1) ||
- ($gender === 'male' && $genderID % 2 === 0)):
- return TRUE;
- else:
- return FALSE;
- endif;
- }
- function isValidCheckSum ($digits, $checksum) {
- $multipliers = array(2, 4, 8, 5, 10, 9, 7, 3, 6);
- $result = 0;
- $remainder = 0;
- for($i = 0; $i < sizeof($digits); $i++):
- $result += $digits[$i] * $multipliers[$i];
- endfor;
- $remainder = $result % 11;
- if (($remainder == $checksum) ||
- ($remainder == 10 && $checksum == 0)):
- return TRUE;
- else:
- return FALSE;
- endif;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement