[kwlug-disc] Hash functions.

Khalid Baheyeldin kb at 2bits.com
Sun Jan 2 13:45:38 EST 2011


You just need to generate a quasi-unique number from a set of variables.

$ php -a
php > $string = "Something or other with date and time";
php > $hash = md5($string);
php > print $hash;
6d1740d6c0c59422b052916acb8ad778

Stuff what you want in $string, be it the store, date, whatever.

Then you can use the $hash value as the key in the database to track
this coupon.

All this would have worked if you have not said it should be short.

But you want it to be short, so the above may be too long.

Would this work? It is based on the microsecond time. If you
have a transaction every few seconds, then it should be OK.
Clashes can happen if you have two at the same exact micro-
second.

$ php -a
php > print uniqid();
4d20c753e4001

Short enough?

Another approach: why not use an autoincrement key in the database table
that is the primary key for the coupon table, and be done with it? The
rest of the data can be cross indexed in that table (store ID, date,
time, description, ...etc.). It will be just a number.
-- 
Khalid M. Baheyeldin
2bits.com, Inc.
http://2bits.com
Drupal optimization, development, customization and consulting.
Simplicity is prerequisite for reliability. --  Edsger W.Dijkstra
Simplicity is the ultimate sophistication. --   Leonardo da Vinci
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://kwlug.org/pipermail/kwlug-disc_kwlug.org/attachments/20110102/b080e843/attachment.htm>


More information about the kwlug-disc mailing list