This was put together as an assessment for a development contract back in August last year.
The code can be found here on GitHub. https://github.com/alancoleman/PHP-PIN-Number-generator
A live working version can be found on this website here. http://www.alancoleman.co.uk/pin/
Rather than repeat myself I’ll just display the contents of the GitHub readme file here…
This application generates Random PIN numbers that satisfy two criteria:
1) Each integer in the PIN is used no more than twice
2) Once a PIN number is generated it can’t be used again
- PHP 5.4 with pdo_mysql extension loaded
- MySQL 5.5
How it works:
- All used PIN Numbers are Queried from the database and populated into an array. This array will be used later to check our new number against. Using an array like this prevents costly revisits to the database further down the script.
- A Random PIN Number is generated.
- The Random PIN Number is checked against itself to make sure that no integer is used more than twice. For example, 3533 would be unacceptable. Once an acceptable Random PIN Number is generated the script moves on.
- The Random PIN Number is now checked against the array of PIN Numbers. If a match is found in the array the check stops and the script returns to stage 2. If no match is found against the array then the Random PIN Number becomes our PIN Number.
- The PIN Number is added to the database to prevent it being used again.
- The PIN Number is presented.