我想从两个字符串( string a和string b. )生成一个唯一的键。string a已经是一个长度为8到20 chars的唯一键,而字符串b并不是相同长度为8到20 chars的唯一键。目标键必须是难以猜测的,唯一的,并有一个固定的长度10个字符。我尝试过一些mad5()、uniqueid()和rand()函数的组合,但每次目标字符串都超过了10 chars的限制。如何获得10 chars的密钥?
发布于 2017-03-07 20:04:17
假设目标密钥存储在db中。
$string1 = 'some_unique_string';
$string2 = 'any_other_string';
do{
$target_string = sub_str(str_shuffle($string1.$string2),0,10);
$sql='select * from table_name where targetKeyColoumn = "'.$$target_string.'"'; //add sql injection prevention
$result = mysqli_query($dbConectionResource, $sql);
$row_cnt = mysqli_num_rows($result);
}while($row_cnt); //if its 0, come out from loop
echo $target_string;希望这能帮上忙。
发布于 2017-03-07 19:56:31
您可以将a和b中的所有字符放入数组中,对数组进行洗牌,然后在从0到9的循环中随机取出它们。
https://stackoverflow.com/questions/42656902
复制相似问题