php - Regarding unique username



My answer is going to address the specific problem you had regarding:

I need a solution from which i generate a unique username with available details also can check that a every student enters in database only one time and have only one username.

Because you stated that you have "first name", "last name" and "birthday" (which might be null) for each student, and need to create a unique username, I'm going to suggest using a checksum.

The real problem is that the data you have cannot in itself guarantee uniqueness, so even the solution I will suggest is not going to work 100% without you implementing some sort of random salt in the user data.

Here is a code snippet to help you see what I'm thinking:

$fname = "John";
$lname = "Doe";
$bday  = "1980-03-01";

$unique_user_string = strtolower($fname . $lname . $bday); // not actually unique if 2 people have same name and birthday
$username = ucfirst(substr($fname, 0, 1)) . ucfirst($lname) . crc32($unique_user_string);

From the above code, you should have a username like: JDoe4246458655

And the nice part is that you can always confirm an existing user by checking the checksum and creating a new one using the user data from your CSV. To get the checksum from the existing username, just do:

$crc = substr($username, -10, 10);

I hope this makes sense.




You can use this function simplly:

$uniqueid2 = md5(uniqid());
$uniqueid = substr($uniqueid2, 0, 8);

And add this $uniqueid variable at the end of their name, so this could lead you to a very unique login.

People are also looking for solutions to the problem: PHP page with embedded HTML displaying extra column


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.