使用 PHP 随机创建密钥
Create keys randomly with PHP
我有一个移动应用程序在我的服务器中请求一个密钥,密钥结构包含 7 个字符如下:
@ + [0-9] + [0-9] + [0-9] + [A-Z] + [A-Z] + [0-9]
@876EU8, @668KI2 .......
虽然密钥最初有七个字符,在本例中是三个数字、两个字母和一个数字,但计算一下最多可以有 676,000 个密钥。
为了生成这些密钥,我在 PHP:
中使用了这段代码
function generateRandomString($length = 2) {
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$randomKeyNumber = rand(100,999);
$randomKeyLetter = generateRandomString();
$randomKeyLast = rand(0,9);
$randomKey = "#".$randomKeyNumber.$randomKeyLetter.$randomKeyLast;//Returns a key like @876TG9
下一个代码检查密钥是否存在于数据库中,如果存在他随机另一个密钥,如果不存在他将密钥插入数据库并且return这个密钥到我的应用程序
此代码完美运行,但假设系统已经生成了总共 650,000 个密钥,对于此代码,它总是会生成相同的密钥,并且它生成的密钥可能还不存在很小。
如何解决这个问题并避免将来出现问题? (按顺序创建密钥没有问题,eg 000AA0, 000AA1, 000AA2, 000AA3 .... 999ZZ9)
你可以做一个 PDO::query()
来发出一个 SELECT COUNT(*)
或者只是一个 SELECT *
语句,其中包含你已经添加的所有键,然后使用 PDOStatement::fetchColumn() 检索将返回的行数(即在本例中,所有行)
这是一个手动示例
<?php
$sql = "SELECT COUNT(*) FROM Keys";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* Issue the real SELECT statement and work with the results */
$sql = "SELECT name FROM fruit WHERE calories > 100";
foreach ($conn->query($sql) as $row) {
print "Name: " . $row['NAME'] . "\n";
}
}
/* No rows matched -- do something else */
else {
print "No rows matched the query.";
}
}
$res = null;
$conn = null;
?>
这是您的案例所需的代码:
<?php
$sql = "SELECT * From Keys";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* and then you get the id of the last one on the list, and to that one you add 1 */
$last_id = $conn->lastInsertId();
$new_id = $last_id + 1;
/* then you insert that in some place inside the key itself, that way you don't need to worry than two keys can be equal */
}
else {
/* No rows matched, just create a key and add to the database here */
}
}
¿>
或者,您可以在 PDO 中结合 countRows 查询 SELECT 语句,它在便携式应用程序 and/or 数据库中并不总是有效,但就像我们对您的应用程序了解不多,我们不知道这是否可行。
我有一个移动应用程序在我的服务器中请求一个密钥,密钥结构包含 7 个字符如下:
@ + [0-9] + [0-9] + [0-9] + [A-Z] + [A-Z] + [0-9]
@876EU8, @668KI2 .......
虽然密钥最初有七个字符,在本例中是三个数字、两个字母和一个数字,但计算一下最多可以有 676,000 个密钥。
为了生成这些密钥,我在 PHP:
中使用了这段代码function generateRandomString($length = 2) {
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$randomKeyNumber = rand(100,999);
$randomKeyLetter = generateRandomString();
$randomKeyLast = rand(0,9);
$randomKey = "#".$randomKeyNumber.$randomKeyLetter.$randomKeyLast;//Returns a key like @876TG9
下一个代码检查密钥是否存在于数据库中,如果存在他随机另一个密钥,如果不存在他将密钥插入数据库并且return这个密钥到我的应用程序
此代码完美运行,但假设系统已经生成了总共 650,000 个密钥,对于此代码,它总是会生成相同的密钥,并且它生成的密钥可能还不存在很小。
如何解决这个问题并避免将来出现问题? (按顺序创建密钥没有问题,eg 000AA0, 000AA1, 000AA2, 000AA3 .... 999ZZ9)
你可以做一个 PDO::query()
来发出一个 SELECT COUNT(*)
或者只是一个 SELECT *
语句,其中包含你已经添加的所有键,然后使用 PDOStatement::fetchColumn() 检索将返回的行数(即在本例中,所有行)
这是一个手动示例
<?php
$sql = "SELECT COUNT(*) FROM Keys";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* Issue the real SELECT statement and work with the results */
$sql = "SELECT name FROM fruit WHERE calories > 100";
foreach ($conn->query($sql) as $row) {
print "Name: " . $row['NAME'] . "\n";
}
}
/* No rows matched -- do something else */
else {
print "No rows matched the query.";
}
}
$res = null;
$conn = null;
?>
这是您的案例所需的代码:
<?php
$sql = "SELECT * From Keys";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* and then you get the id of the last one on the list, and to that one you add 1 */
$last_id = $conn->lastInsertId();
$new_id = $last_id + 1;
/* then you insert that in some place inside the key itself, that way you don't need to worry than two keys can be equal */
}
else {
/* No rows matched, just create a key and add to the database here */
}
}
¿>
或者,您可以在 PDO 中结合 countRows 查询 SELECT 语句,它在便携式应用程序 and/or 数据库中并不总是有效,但就像我们对您的应用程序了解不多,我们不知道这是否可行。