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 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题