Here is an example of generating a unique string in PHP very useful in some cases especially when you work with large amounts of rows in a db table and you want unique and "hard to find" keys for every row.
My usual method to do this was using the strtotime() function, with "now" as the argument and to the result I was applying the md5() to get a random unique id. The result is fine until you run the script more then 1 time in a second when you get the same string. Here is the new way I do to get a real unique ID not depending on time:

  1. function unique_id(){
  2. $better_token = md5(uniqid(rand(), true));
  3. $unique_code = substr($better_token, 16);
  4. $uniqueid = $unique_code;
  5. return $uniqueid;
  6. }
  8. $id = unique_id();
  9. echo $id;

This time I use the rand() and uniqid() functions.