PHP & mySQL (i): How to create a random user id?

Tell me, if I wanted to give each user who registered on my site a unique identifier. It seems to me that if I wanted to do this, I would have to: create a random number for id, check if this identifier exists in the database, if it exists, create another random number and send another request to see if it exists etc.

This can go on for centuries. Besides having incremental id, is there a decent way to do this?

+6
source share
6 answers

The best way to do this is through the auto-increment function, if you really don't want to use such a function so that you can use uniqid ();

Basically you generate a unique identifier based on milliseconds, if you enter a unique unique prefix into it, it will generate a unique identifier.

echo uniqid('prefix'); 

This way, you do not have to check after generating the identifier if it already exists or not. You can be sure that it is unique.

For more information check out this url http://php.net/uniqid !

+2
source

You can use the rand () function. It will generate a random number between the two.

 rand(0000,9999) 

It will generate a number from 0 to 9999.

To check if it exists:

 $id = rand(0000,9999); /* CREATE YOUR MYSQL CONNECTION */ $user_list = mysql_query("SELECT * FROM users"); while ($user = mysql_fetch_array($user_list)) { if ($id == $user['id']) { echo('Already exist.'); } else { /* YOUR CODE */ } } 

This is the way I did it ...

+1
source

First of all, I agree with the comments. This is all the top-level code, and if you use it to make it look interesting, you should really rethink your priorities.

But, if you still need to; here is something:

 function uid() { mt_srand((double)microtime()*1000000); $token = mt_rand(1, mt_getrandmax()); $uid = uniqid(md5($token), true); if($uid != false && $uid != '' && $uid != NULL) { $out = sha1($uid); return $out; } else { return false; } } 

Basically, it creates a lot of random numbers to create a marker for uniqueid, and then it is sha. Maybe overhead, but you can be sure that you will never generate a double uid.

Fabian.

+1
source

If you have a line of 15 numbers that you look at up to 999 trillion, I doubt that it will work for "ages", considering there are almost 7 billion people on the planet.

0
source

Should the identifier be numeric? By switching to alphabetic characters, you will get much more entropy. 6-digit number - 1 000 000 possibilities, 6-digit alphanumeric string - 2 176 782 336 possible. Make it mixed case alphanumeric, and it's a jump to 15.625 billion.

This is how I usually generate unique strings that are as short as possible:

 $chars = 'abcdefghijklmnopqrstuvwrxyzABCDEFGHIJKLMNOPQRSTUVWRXYZ0123456789'; mt_srand((double)microtime()*1000000); $id = ''; do { $id .= $chars[mt_rand(0, strlen($chars) - 1)]; } while (isIdTaken($id)); var_dump($id); 

You need to create many elements with this identifier style before you get more than 3 or 4 characters.

0
source

I know this is late for this answer, but the simplest solution is to generate a random number and I am sure that it will be unique, 100%

 $uid = uniqid().date("Ymdhhis"); 
0
source

Source: https://habr.com/ru/post/905154/


All Articles