php 如何在购物车中生成唯一的交易 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18121749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:01:14  来源:igfitidea点击:

How to generate a unique transaction id in a shopping cart

phpshopping-cart

提问by andromeda

I am working on a shopping cart and a local payment gateway that requires each transaction to have a unique order id. This id is supposed to be saved in the database against the order details and sent as a variable to the payment gateway. My dilemma is on how to generate the unique id and how to save it before sending my variables to the gateway so that I can check that it matches when a response is received from the gateway's IPN. The variable in question is show below:

我正在开发一个购物车和一个本地支付网关,它要求每笔交易都具有唯一的订单 ID。这个 id 应该根据订单详细信息保存在数据库中,并作为变量发送到支付网关。我的困境是如何生成唯一 id 以及如何在将我的变量发送到网关之前保存它,以便我可以在从网关的 IPN 收到响应时检查它是否匹配。有问题的变量如下所示:

$reference = $_POST['reference'];//unique order id of the transaction, generated by merchant

回答by Channaveer Hakari

Here is how i used to generate the random key, even though its bit lengthy you can achieve unique random key.

这是我用来生成随机密钥的方法,即使它有点长,您也可以获得唯一的随机密钥。

$id.time().uniqid(mt_rand(),true)

uniqid() - this will generate the 13 character uniqid

uniqid() - 这将生成 13 个字符的 uniqid

mt_rand() - generates random number by using Mersenne Twister algorithim and is 4 times more fast then rand()

mt_rand() - 使用 Mersenne Twister 算法生成随机数,比 rand() 快 4 倍

uniqid(mt_rand()) - prefixing the random generate number with uniqueid

uniqid(mt_rand()) - 在随机生成数字前加上 uniqueid

uniqid(mt_rand(), true) - to get more strong random i am enabling entropy of uniqid by setting second parameter to 'true'

uniqid(mt_rand(), true) - 为了获得更强的随机性,我通过将第二个参数设置为“true”来启用 uniqid 的熵

$id.time().uniqid(mt_rand(), true) - here $id may be userid, productid or any id which you want to choose, time() will generate current unix time stamp, lastly i will append with my uniqid(...)

$id.time().uniqid(mt_rand(), true) - 这里 $id 可以是 userid、productid 或任何你想要选择的 id,time() 将生成当前的 unix 时间戳,最后我将附加我的 uniqid (……)

回答by Dexa

Do not use md5() on uniqueid() it will actually loose on security. To append or prepend md5 value is OK.

不要在 uniqueid() 上使用 md5() 它实际上会失去安全性。添加或添加 md5 值是可以的。

That said I would stick to http://www.php.net/manual/en/pdo.lastinsertid.phpto get newly inserted ID, but also generate uniqueid() with prefix parameter set to lets say 'username_' so that way it should be pretty much unique.

也就是说,我会坚持使用http://www.php.net/manual/en/pdo.lastinsertid.php来获取新插入的 ID,但也会生成带有前缀参数设置的 uniqueid() 以这样说它应该非常独特。

回答by Ben

I would say that the way to make the most unique ID would be a combination of the current timestamp (unix using time()) and then append and/or prepend it with something like the current users username. This way it is almost impossible to create a duplicate. You could also add in the last inserted ID as well. In my opinion the combination of these three is a simple method and will make it almost impossible for duplicates due to the fact that time() will change every second and a user is very unlikely to be able to add 2 orders at the exact same second.

我会说,制作最独特 ID 的方法是结合当前时间戳(unix 使用 time()),然后附加和/或添加类似当前用户用户名的内容。这样几乎不可能创建副本。您还可以添加最后插入的 ID。在我看来,这三者的结合是一种简单的方法,并且几乎不可能重复,因为 time() 每秒都会改变,用户不太可能在同一秒添加 2 个订单.

回答by Mircea Soaica

Insert the order details into a table. The primary key of that table should have AUTO INCREMENT property set. Then retrieve the last inserted id http://php.net/manual/en/function.mysql-insert-id.php

将订单详细信息插入表中。该表的主键应设置 AUTO INCREMENT 属性。然后检索最后插入的id http://php.net/manual/en/function.mysql-insert-id.php

回答by shairya

Besides primary key of order table, a combination of a string, year, month, day and 4 digit no. can be useful.

除了订单表的主键,字符串、年、月、日和4位数字的组合。可能有用。

eg.

例如。

orders placed on 8th Aug 2013 could have following order numbers: OS201308080001 OS201308080002

2013 年 8 月 8 日下的订单可能有以下订单号:OS201308080001 OS201308080002

orders placed on 9th Aug 2013 could have following order numbers: OS201308090001 OS201308090002 OS201308090003

2013 年 8 月 9 日下的订单可能有以下订单号: OS201308090001 OS201308090002 OS201308090003

where OS could be initial's of your store name, next 4 digits are year, next 2 digits are months, next 2 digits are date and next 4 digits (starting from 0001 every day) can identify number of orders each day.

其中 OS 可以是您商店名称的首字母,接下来的 4 位数字是年份,接下来的 2 位数字是月份,接下来的 2 位数字是日期,接下来的 4 位数字(每天从 0001 开始)可以识别每天的订单数量。

回答by Wiggler Jtag

If you really want an unique-id, here is the code which I am using. This simple creates 7 characters string and if it already exists in database -> regenerate new, until it is 100% unique. uniqid() is not really unique id, you could put there your own prefix which could be mixed with my code, but alone uniqid() with putting it to base64 and md5 and sha1 and I dont know what else IS NOT unique.

如果你真的想要一个唯一的 id,这是我正在使用的代码。这个简单的创建 7 个字符的字符串,如果它已经存在于数据库中 -> 重新生成新的,直到它是 100% 唯一的。uniqid() 并不是真正唯一的 id,你可以在那里放你自己的前缀,它可以与我的代码混合,但单独 uniqid() 将它放入 base64 和 md5 和 sha1,我不知道还有什么不是唯一的。

function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
    $str = '';
    $count = strlen($charset);
    while ($length--) {
        $str .= $charset[mt_rand(0, $count-1)];
    }

    return $str;
}

$unique_id = randString(7);  
while(mysqli_num_rows(mysqli_query($con, "SELECT unique_id FROM table WHERE unique_id = '".mysqli_real_escape_string($con, $unique_id)."'")) > 0) { 
   $unique_id = randString(7);
}
//insert this id into database and use it

回答by Antoine Augusti

Something based on a table of your database? A column with AUTO_INCREMENT. You can fetch the last insert ID with PDO with this: http://www.php.net/manual/en/pdo.lastinsertid.php

基于数据库表的东西?带有 AUTO_INCREMENT 的列。您可以使用 PDO 获取最后一个插入 ID:http: //www.php.net/manual/en/pdo.lastinsertid.php

Or something like

或者类似的东西

$reference = sha1(md5(time()));