Merry Christmas and Happy Holidays

Xhtml/CSS No Comments »

Holidays are here, a moment of joy and good time for all of us in the middle of friends and family. I wish Happy Holidays and Merry Christmas to everyone and if this will be the last post of this year, I also say: Happy New Year!

Generate a unique string in PHP

PHP 5 Comments »

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:

CODE:
  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. }
  7.  
  8. $id = unique_id();
  9. echo $id;

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

Theme designed by N.Design Studio
Entries RSS Comments RSS Log in