如何生成唯一用户名-PHP
我在数组中有用户名字符串。我想生成一个数组中不存在的唯一用户名字符串。(可能用户名后面有一些数字) 我如何做到这一点 我总结了代码:如何生成唯一用户名-PHP,php,Php,我在数组中有用户名字符串。我想生成一个数组中不存在的唯一用户名字符串。(可能用户名后面有一些数字) 我如何做到这一点 我总结了代码: function generate_unique_username(){ $firstname = "james";//data coming from user $lastname = "oduro";//data coming from user $new_username = $firstname.$last
function generate_unique_username(){
$firstname = "james";//data coming from user
$lastname = "oduro";//data coming from user
$new_username = $firstname.$lastname;
$usersnames = array("james39","oduro32","kwame93","elvisasante","frimpong32","edward32","jamesoduro");
//Loop through ARRAY usernames and check elements against VAR $new_username
if (in_array($new_username, $usersnames)) {
//generate new username which is not inside array
//the new generated string should also be check against array to ensure is doens not exit.
}else{
return $new_username;
}
}
谢谢。从存储的数组生成用户名不是一个好做法,我建议您使用数据库 如果使用的是数据库而不是数组,则可以使用最佳方法生成唯一用户名,如下所示:
function generate_unique_username(){
$firstname = "james";//data coming from user
$lastname = "oduro";//data coming from user
$new_username = $firstname.$lastname;
/* Note: writing here pseudo sql code, replace with the actual php mysql query syntax */
$query = "SELECT COUNT(id) as user_count FROM user WHERE username like '%".$new_username."%'";
$result = mysql_query($query);
$count = $result['user_count'];
if(!empty($count)) {
$new_username = $new_username . $count;
}
return $new_username;
}
我认为在这种情况下,您应该首先尝试为用户分配更酷的用户名,然后在失败时使用数字后缀。这是我可以使用的方法。您可能需要将代码更改为您更喜欢的、更安全的mysqli调用,就像使用PDO或mysqli prepared语句一样
//function that will be used to figure out if the user name is available or not
function isAvailable($userName){
global $mysqli;
$result = $mysqli->query("SELECT id FROM users WHERE user_name='$userName'") or die($mysqli->error());
// We know username exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {
//echo 'User with this username already exists!';
return false;
}else{
return true;
}
}
function generate_unique_username($firstname, $lastname, $id){
$userNamesList = array();
$firstChar = str_split($firstname, 1)[0];
$firstTwoChar = str_split($firstname, 2)[0];
/**
* an array of numbers that may be used as suffix for the user names index 0 would be the year
* and index 1, 2 and 3 would be month, day and hour respectively.
*/
$numSufix = explode('-', date('Y-m-d-H'));
// create an array of nice possible user names from the first name and last name
array_push($userNamesList,
$firstname, //james
$lastname, // oduro
$firstname.$lastname, //jamesoduro
$firstname.'.'.$lastname, //james.oduro
$firstname.'-'.$lastname, //james-oduro
$firstChar.$lastname, //joduro
$firstTwoChar.$lastname, //jaoduro,
$firstname.$numSufix[0], //james2019
$firstname.$numSufix[1], //james12 i.e the month of reg
$firstname.$numSufix[2], //james28 i.e the day of reg
$firstname.$numSufix[3] //james13 i.e the hour of day of reg
);
$isAvailable = false; //initialize available with false
$index = 0;
$maxIndex = count($userNamesList) - 1;
// loop through all the userNameList and find the one that is available
do {
$availableUserName = $userNamesList[$index];
$isAvailable = isAvailable($availableUserName);
$limit = $index >= $maxIndex;
$index += 1;
if($limit){
break;
}
} while (!$isAvailable );
// if all of them is not available concatenate the first name with the user unique id from the database
// Since no two rows can have the same id. this will sure give a unique username
if(!$isAvailable){
return $firstname.$userId;
}
return $availableUserName;
}
//Get the unique user id from your database, for now let's go with 30
$userId = 30;
echo generate_unique_username('john', 'oduro', $userId);
另外,如果用户不喜欢自动生成的值,最好提供一个后备功能,用户可以将用户名更改为任何其他唯一值。您的解决方案无法很好地扩展。使用这种流,您需要获取数据库中存在的所有用户名,将它们存储在数组中,并对照整个数组进行检查。如果您的网站拥有10-5万用户(即使99%的用户处于非活动状态),注册页面的性能将严重下降。预生产版本
$i=0;do{$new\u username\u full=$new\u username.mt\u rand(199999);}while(在数组中($new\u username\u full,$usernames)&&&$i++问题是:为什么?如果这是db相关的,你应该在电子邮件地址上创建一个唯一的索引。@jamesOduroI可以看到你将变量$new_username与一个随机数连接起来。我认为这里仍然存在一个问题……你如何确定生成的新用户名是否已经在里面?我觉得你将使用更多的资源RCE超出要求,查询可能会成为一个问题。正如我在之前的评论中所说,我认为最好是在电子邮件(地址)列上创建一个唯一的索引,但这只是“我的2美分”。如果是注册页面,可以使用包含编码版本(电子邮件地址)的验证链接,和一次性令牌密钥。同样,只是一个建议。@jamesoduroth此答案中显示的代码假定已使用的用户名中没有一个等于$firstname.$lastname.$count
。它不能保证用户名是唯一的。例如,可能有5个用户名以jamesoduro开头,其中一个是jamesoduro5。此代码将建议使用已经使用过的jamesduro5。即使使用$count+1
而不是$count
也会有所不同,因为其中一个用户名可能是jamesoduro6。Hi@kiamlaluno,如果有5个用户名以jamesoduro
开头,如果您注意到该查询,它将获取该用户的名字和姓氏的计数组合,因此它将始终确保唯一的用户名。只有当所有用户名都使用该函数生成且以后无法更改时,它才起作用。否则,计算有多少用户名包含jamesoduro,并在名称后添加该数字不会创建唯一的用户名。例如,数据库中已有的用户名是jamesoduro1、jamesoduro2、jamesoduro3、jamesoduro5、jamesoduro6。名称为5,因此该函数表示已使用的jamesoduro5。