Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you suggest unique usernames to user in PHP?

Tags:

php

mysql

Let's say the username someone wants is taken, then I want the system to suggest a list of available usernames, say 5 suggestions.

Now, I can generate several usernames based on permutations and combinations of data I already have, like their name, birthday, e.t.c. But how do I make sure those generated usernames are available? I mean, if for each generated username, I have to query the database to check availability, in the worst case scenario this might become something like an infinite loop.

Any ideas?

like image 779
Bluemagica Avatar asked Aug 17 '11 07:08

Bluemagica


People also ask

How to create Unique username in php?

To Generate Random Username It Takes Only three Steps:-In get_username() function we get the value of name and then make an ajax call to 'do_signup. php' to get related random username and then display and store the value in hidden form field. You may also like check username and email availability from database.

How can I see the username in php?

The register. php page asks for the desired username, email, and password of the user, and then sends the entered data into the database, once the submit button is clicked. After this, the user is redirected to the index. php page where a welcome message and the username of the logged-in user is displayed.

Why do usernames have to be unique?

Why you need a unique username for every account. The main issue with usernames is that they are public, not hidden like your passwords. Reusing the same username makes it easier for malicious actors to build your online profile. It also makes it easier for advertisers and random people to find and track you online.


1 Answers

I suspect the question is a base one so I'll provide a simple example:

<?php

// Untested code and only one of the many possible ideas
$suggestions = array(
    'foobar' => TRUE,
    'foo1974' => TRUE,
    'foo37' => TRUE,
    'drfoo' => TRUE,
    'mrfoo' => TRUE,
);


$params = $placeholders = array();
foreach(array_keys($suggestions) as $position => $username){
    $params['u' . $position] = $username;
    $placeholders[] = ':u' . $position;
}
$sql = 'SELECT username
    FROM user
    WHERE username IN (' . implode(', ', $placeholders) . ')';

$res = $conn->prepare($sql);
$res->execute($params);
while( $row = $res->fetch(PDO::FETCH_ASSOC) ){
    $suggestions[ $row['username'] ] = FALSE;
}


foreach($suggestions as $username => $available){
    if($available){
        // ...
    }
}

Edit:

The only way to provide an infinite list of available names is to use a very simple rule such as adding a consecutive number. In such case, you can try something like this:

SELECT username
FROM user
WHERE username REGEXP '^foo[0-9]+'

... and then:

$username = 'foo';

$suggestions = array();
$count = 0;
$names_left = 5;
while($names_left>0){
    $count++;

    if( !in_array($username . $count, $names_taken) ){
        $suggestions[] = $username . $count;
        $names_left--;
    }
}
like image 84
Álvaro González Avatar answered Oct 21 '22 13:10

Álvaro González