dongtangu6144 2014-09-18 08:16
浏览 131
已采纳

mt_rand + time()生成密钥的准确性和唯一性如何用作预订参考ID? [关闭]

I am thinking to use $uniqueId = substr(md5(time() * mt_rand()),0,5); to generate unique IDs for booking reference. But how about the duplication of this generated ID? what if the database already have the same ID that was generated by this? How many percentage of chance to there will be generating same IDs if I use this?

  • 写回答

1条回答 默认 最新

  • dougan0529 2014-09-18 08:29
    关注

    From a human point of view, an md5 wouldn't make a good booking reference - it'd be quite awkward to relay over the phone, for example.

    But from a development point of view, you perhaps don't want to use a straight auto-increment, as it could give away information about the size of the business (a low number might suggest very few customers and erode customer confidence).

    Options include:

    • Using an element of the date and something relating to the user to generate a reference. Such as 2014/09/CUSTOMERSURNAME01

    • Using an element of the date and a random

    In all options, a quick call to the DB to check for uniqueness will give you confidence that you'll never create a duplicate. It wouldn't add much overhead to check, and if you get a duplicate try again. The chances of having to try again twice would be very low.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集