我正在设计一个购物项目,正在考虑使用 unix 时间戳作为订单 ID。我知道我可以使用 mySQL 增量数字作为订单 ID,例如 10001、10002 等,但我不想让每个人都知道实际有多少订单。
这样做安全吗?我显然预计每秒不会超过 1 个订单,所以我应该是安全的,对吧?
function check_number(){
$unique_number = time();
$exists = $this->count_rows('orders', "WHERE order_id='" . $unique_number . "'");
if ($exists >0){
$results = check_number();
}
else{
$results = $unique_number;
return $results;
}
}
最佳答案
一个更强大的解决方案是使用 PHP 的 uniqid()功能。它隐藏了您系统中的订单数量,但如果两个或更多订单碰巧同时进入,它也不会崩溃。
至于实际问题:不,使用 unix 时间戳作为半随机事件的唯一 ID 是不安全的,例如用户下订单。无论发生碰撞的可能性有多大,既然可以轻松避免,为什么还要留下碰撞的可能性呢?
关于php - 使用 unix 时间戳作为订单 ID 有什么缺点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30249089/