Unique Code Generator


/ Published in: PHP
Save to your folder(s)

For a project I needed to generate a unique code for each user the client would input manually. Each user would have four fields (first name, last name, email, zipcode). Omitting the email addresses ensures unique output because we reduce the chances of characters like; '@', '.', and strings like 'com' being reused everytime. It's simple, it's efficient and it's not overcomplicated.


Copy this code and paste it in your HTML
  1. //unique is built by concatenating the user input (first, last, zip)
  2. function makeCode($unique) {
  3.  
  4. //specify output string length
  5. $length = 5;
  6.  
  7. //start with empty sting
  8. $string = '';
  9.  
  10.  
  11. for ($i = 0; $i < $length; $i++) {
  12. $string .= $unique[rand(0, strlen($unique) - 1)];
  13.  
  14. }
  15.  
  16. return $string;
  17.  
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.