php-instanceid

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.

Supported Platforms

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.

Usage

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');
?>

Installation

Linux (Ubuntu)

  1. The PHP development tools must be installed, on Ubuntu/Debian this package is called php-dev

  2. Build the extension from source code directory:

    $ cd ext/instanceid
    $ phpize
    $ ./configure
    $ make && make test
  3. Install the extension:

    $ sudo make install
  4. Enable the extension:

    $ echo "extension=instanceid.so" | sudo tee /etc/php/PHP_VERSION/mods-available/instanceid.ini
    $ sudo phpenmod instanceid

Windows

  1. Visual Studio and the PHP SDK must be installed

  2. We assume that the php-instanceid source code repository is located at directory C:\source\php-instanceid

  3. Extract the PHP sources to directory C:\php-src

  4. 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
  5. Copy the generated php_instanceid.dll to your PHP extensions directory, for example:

    C:\XAMPP\php\ext\php_instanceid.dll
  6. Enable the extension by adding the following line to your php.ini file:

    extension=php_instanceid.dll

Configuration

Configuration options for php-instanceid can be set in the php.ini file:

Version History

Version 1.3.0

License

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