我有一个登录过程,我希望尽可能多地预先填写。用户名必须是唯一的,所以我必须得到一个独特的“随机”字符串。我该怎么做?
我正在考虑这样的事情:
function getRandomString($length = 5) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[rand(0, strlen($characters)-1)];
}
return $string;
}
function getUniqueVarchar($prefered) {
// Try all combinations that would make sense to the user. Eventually give an array and try to combine elements like first name / last name / birthday
$usernameTry = $prefered
$exists = Does the username $usernameTry exist?
if (!$exists) return $usernameTry;
// Maybe a year exists?
for($i=50;$i<99;$i++) {
// $exists = Does the username $usernameTry = $prefered.$i exist?
if (!$exists) return $usernameTry;
}
// Try random strings
for($i=0;$i<10;$i++) {
// $exists = Does the username $usernameTry = getRandomString() exist?
if (!$exists) return $usernameTry;
}
return false;
}
在“//尝试随机字符串”中存在比我更好的东西吗? MySQL(或其他数据库系统)是否有类似内置的东西?