Posted By

fmdfrank on 06/15/12


Tagged

php functions unique codes


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

DanielLucia
tux-world


Unique Code Generator


 / Published in: PHP
 

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.

  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  

You need to login to post a comment.