Hướng dẫn how to generate unique username in php - cách tạo tên người dùng duy nhất trong php

Tôi nghĩ rằng trong trường hợp này, trước tiên bạn nên thử và gán tên người dùng mát hơn cho người dùng sau đó khi thất bại, bạn sẽ sử dụng một hậu tố số. Đây là một cách tiếp cận tôi có thể sử dụng. Bạn có thể cần thay đổi mã thành cuộc gọi MySQLI được ưa thích hơn và bảo mật như sử dụng câu lệnh PDO hoặc MySQLI được chuẩn bị.

//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);

Ngoài ra, thật tuyệt khi cung cấp một tính năng dự phòng trong đó người dùng có thể thay đổi tên người dùng của họ thành bất kỳ giá trị duy nhất nào khác, trong trường hợp họ không thích giá trị tự động tạo.

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

/**
* Hàm đệ quy để tạo tên người dùng duy nhất.
*
* Nếu tên người dùng đã tồn tại, sẽ thêm một hậu tố số sẽ tăng cho đến khi tìm thấy tên người dùng duy nhất.
*
* Nếu tên người dùng đã tồn tại, sẽ thêm một hậu tố số sẽ tăng cho đến khi tìm thấy tên người dùng duy nhất.
*
* Nếu tên người dùng đã tồn tại, sẽ thêm một hậu tố số sẽ tăng cho đến khi tìm thấy tên người dùng duy nhất.
* @param chuỗi $ tên người dùng
* @return chuỗi tên người dùng duy nhất. generate_unique_username( $username ) {
*/ $i;
functionGenerate_unique_username ($ username) { ( null === $i ) {
tĩnh $ i;i = 1;
if (null === $ i) {else {
$ i = 1;i++;
} khác {
$ i ++; ( ! username_exists( $username ) ) {
} $username;
} khác {
$ i ++;new_username = sprintf( '%s-%s', $username, $i );
} ( ! username_exists( $new_username ) ) {
if (! username_exists ($ username)) { $new_username;
if (null === $ i) {else {
$ i = 1; call_user_func( __FUNCTION__, $username );
} khác {
$ i ++;

}

if (! username_exists ($ username)) {generating the username with the first and lastname concatenated. Then use the LIKE clause in MySql to find any matching usernames. This will generate an array of usernames that is an exact match or like the auto-generated username.

Làm thế nào để tôi có được một tên người dùng độc đáo trong Python?

chia () nếu len (full_name)> 1: first_letter = full_name [0] [0] ba_letters_surname = full_name [-1] [: 3].Rjust (3, 'x') số = '{: 03d}'.Định dạng (ngẫu nhiên. Randrange (1.999)) username = '{} {} {}'.