我想在我的数据库中插入一个唯一的id,并通过json编码输出一些文本。像这样我的代码工作正常,我的输出是“你好”:
$json = array(
'example' => 'hello',
);
echo json_encode($json);
$uniqueID = "123";
$pdo = $db->prepare('INSERT INTO example (unique_id) values(:unique_id) ');
$pdo->execute(array(
':unique_id' => $uniqueID,
));
但是当我想生成唯一的id时,就会出现一些我无法弄清楚的错误:
$json = array(
'example' => 'hello',
);
echo json_encode($json);
$random_id_length = 10;
$uniqueID = crypt(uniqid(rand(),1));
$uniqueID = strip_tags(stripslashes($uniqueID));
$uniqueID = str_replace(".","",$uniqueID);
$uniqueID = strrev(str_replace("/","",$uniqueID));
$uniqueID = substr($uniqueID,0,$random_id_length);
$pdo = $db->prepare('INSERT INTO example (unique_id) values(:unique_id) ');
$pdo->execute(array(
':unique_id' => $uniqueID,
));
像这样,所有内容都正确地插入到数据库中。但我没有得到任何结果。
答案 0 :(得分:2)
我认为你得到的错误是:
PHP: crypt(): No salt parameter was specified. You must use a randomly generated salt and a strong hash function to produce a secure hash.
来自docs:
salt参数是可选的。但是,crypt()创建一个没有salt的弱哈希。 PHP 5.6或更高版本在没有它的情况下引发E_NOTICE错误。确保指定足够强的盐以提高安全性。
docs还建议改为使用password_hash
:
password_hash()使用强哈希,生成强盐,并自动应用适当的回合。 password_hash()是一个简单的crypt()包装器,与现有的密码哈希兼容。鼓励使用password_hash()。
所以尝试改变:
$uniqueID = crypt(uniqid(rand(),1));
要:
$uniqueID = password_hash(uniqid(rand(),1), PASSWORD_DEFAULT);