0

I couldn't find the actual function's code as I am interested in how it creates the id, e.g. where it gets the time from.

user1166981
  • 1,668
  • 7
  • 25
  • 40

4 Answers4

3

If you're still interested in the source, it's in the public git repository: https://github.com/php/php-src/blob/master/ext/standard/uniqid.c

gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
sec = (int) tv.tv_sec;
usec = (int) (tv.tv_usec % 0x100000);

/* The max value usec can have is 0xF423F, so we use only five hex
* digits for usecs.
*/
if (more_entropy) {
    spprintf(&uniqid, 0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg(TSRMLS_C) * 10);
} else {
    spprintf(&uniqid, 0, "%s%08x%05x", prefix, sec, usec);
}

RETURN_STRING(uniqid, 0);
lethal-guitar
  • 4,289
  • 1
  • 17
  • 38
2

Which function? I don't believe there's a unique_id() function in PHP. The code for uniqid() is in ext/standard/uniqid.c.

You can search for this kind of thing on Github by narrowing down the repository. For example, searching for repo:php/php-src uniqid will find references to uniqid in the php repo.

There's an example of this, and other Github search syntax, in the answer to this earlier question.

Community
  • 1
  • 1
Matt Gibson
  • 37,268
  • 9
  • 91
  • 125
1

You'll find the source code for uniqid() in https://github.com/php/php-src/blob/master/ext/standard/uniqid.c

Mark Baker
  • 205,174
  • 31
  • 336
  • 380
0

This might help from the manual: http://php.net/manual/en/function.uniqid.php

It explains the uniqueness is taken from the time in microseconds.

webnoob
  • 15,384
  • 12
  • 79
  • 160