How to create unique identifiers for my scaled servers with PHP?

I am using the PHP uniqid () function on my server. It should be something like micro-time. Therefore, I believe that this is a unique FOR ONE server. Is it correct?

How can I get a unique identifier if I scale my server using loadbalancer? I need a string less than 31 characters long.

thank

+3
source share
5 answers

I would suggest combining several sources of entropy. Thus, you will not rely on some assumptions (another local IP address) or luck (two servers will not do the exact same thing in the same nanoteme).

Things that come to my mind (and are quite portable, but not platform specific):

  • nanotime,
  • temp ,
  • script datetime stamp,
  • no-op ,
  • ...

-, 30- (, 30 md5sum strval() ).

+2

uniqid() , . .

string uniqid ([ string $prefix = "" [, bool $more_entropy = false ]] )

prefix
, , , .
13 . more_entropy TRUE, 23 .

:

$serverId = str_replace('.', '', $_SERVER["SERVER_ADDR"].$_SERVER["SERVER_PORT"]);
$uid      = substr(uniqid($serverId, true), 0, 30);

uuid(): http://cakebaker.42dh.com/wp-content/uploads/2007/01/uuid_component_2007-01-24.zip

+2

, , .

prefix .

more_entropy 7 , 256 ** 7 .

+1

You must set the prefix for a unique string (unique to each server on your system). Examples are hostname or IP address. Keep this value less than 17 characters (or 7 if you use extra entropy).

0
source

Try

$uid = uniqid($serverId, true);

This will be the prefix of each $uidusing $serverId.

-1
source

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


All Articles