Provides a unique (cryptographically secure pseudorandom) identifier
per server instance. This identifier is provided via the
PHP_INSTANCE_UNIQID
constant.
The unique instance identifier is generated via the getentropy()
or the BCryptGenRandom()
system function, i.e. via a cryptographically secure random
number generator. Therefore, the identifier will be different, with
extremely high probability, for each server instance. Also, the unique
identifier is generated once, when the
instanceid
extension is initialized, so that the identifier
remains unchanged across multiple PHP
script invocations, for as long as the server instance is running. A new
unique identifier is generated when the server is restarted. For
convenience, the “binary” identifier is converted to a Base64-encoded
string.
This extension supports PHP versions 7.4
or newer,
including the current 8.*
series. It can be built on
Microsoft Windows, GNU/Linux (e.g. Ubuntu), FreeBSD or Mac OS X.
The unique identifier for the current instance, in its Base64-encoded
form, is available via the
PHP_INSTANCE_UNIQID
constant:
<?php
echo 'PHP_INSTANCE_UNIQID: "' . PHP_INSTANCE_UNIQID . '"' . PHP_EOL;
?>
Additionally, the unique identifier is available in its raw “binary”
form via the PHP_INSTANCE_UNIQID_BINARY
constant:
<?php
echo 'PHP_INSTANCE_UNIQID_BINARY: "' . bin2hex(PHP_INSTANCE_UNIQID_BINARY) . '"' . PHP_EOL;
?>
The unique instance identifier is suitable for deriving additional cryptographic keys, using a key derivation function (KDF), e.g.:
<?php
$aes_key = hash_hkdf('sha256', PHP_INSTANCE_UNIQID_BINARY, 16, 'my-aes-key');
?>
The PHP development tools must be installed, on
Ubuntu/Debian this package is called php-dev
Build the extension from source code directory:
$ cd ext/instanceid
$ phpize
$ ./configure
$ make && make test
Install the extension:
$ sudo make install
Enable the extension:
$ echo "extension=instanceid.so" | sudo tee /etc/php/PHP_VERSION/mods-available/instanceid.ini
$ sudo phpenmod instanceid
Visual Studio and the PHP SDK must be installed
We assume that the php-instanceid source code
repository is located at directory
C:\source\php-instanceid
Extract the PHP
sources to directory C:\php-src
Open the PHP SDK terminal window,
e.g. phpsdk-vs16-x64.bat
, and build the extension:
cd /d C:\php-src
buildconf.bat --add-modules-dir=C:\source\php-instanceid\ext
configure.bat --disable-all --enable-cli --enable-instanceid
nmake
Copy the generated
php_instanceid.dll
to your PHP
extensions directory, for example:
C:\XAMPP\php\ext\php_instanceid.dll
Enable the extension by adding the following line to your
php.ini
file:
extension=php_instanceid.dll
Configuration options for php-instanceid can be set
in the php.ini
file:
instance_uniqid.size
(Default:
24)
Specifies the size of the generated identifier, in octets. The length of
the resulting Base64-encoded identifier string, in characters, is equal
to (size / 3) * 4
. This size must be in the range between
12 (inclusive) and 96 (exclusive). It
shall be divisible by 3, values not
divisible by 3 are clipped!
instance_uniqid.safe
(Default:
yes)
Specifies whether the “URL safe” encoding is used to generate the
Base64-encoded identifier string. Set to
no
for “standard” (RFC 4648) Base64
encoding.
Starting with this version, php-instanceid uses
a shared memory segment to maintain the unique identifier per
user rather than per process.
This improves support for usage scenarios with multiple separate PHP
processes (e.g. FastCGI).
Implementation note: Uses the shm_open()
function on Linux/Unix systems, or the CreateFileMappingW()
function on Microsoft Windows.
The maximum length of the unique identifier has been reduced to 96 bytes
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to http://unlicense.org/
Code repository:
https://bitbucket.org/php-uniqueid/php-instanceid/
Author:
https://stackoverflow.com/users/23479232/schraubstock1990