.. _php-betterrand: .. _use-random\_int(): Use random_int() ++++++++++++++++ `rand() `_ and `mt_rand() `_ should be replaced with `random_int() `_. At worse, `rand() `_ should be replaced with `mt_rand() `_, which is a drop-in replacement and `srand() `_ by `mt_srand() `_. `random_int() `_ replaces `rand() `_, and has no seeding function like `srand() `_. Other sources of entropy that should be replaced by `random_int() `_ : `microtime() `_, `uniqid() `_, `time() `_. Those a often combined with hashing functions and mixed with other sources of entropy, such as a salt. Since PHP 7, `random_int() `_ along with `random_bytes() `_, provides cryptographically `secure `_ pseudo-random bytes, which are good to be used when security is involved. `openssl_random_pseudo_bytes() `_ may be used when the ``OpenSSL`` extension is available. .. code-block:: php See also `CSPRNG `_ and `OpenSSL `_. Connex PHP features ------------------- + `random `_ Suggestions ___________ * Use random_bytes() and randon_int(). At least, use them as a base for random data, and then add extra prefix and suffix, and a hash call on top. Specs _____ +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Short name | Php/BetterRand | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Rulesets | :ref:`All `, :ref:`Analyze `, :ref:`CE `, :ref:`CI-checks `, :ref:`Changed Behavior `, :ref:`CompatibilityPHP71 `, :ref:`Security ` | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Exakat since | 0.8.4 | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | PHP Version | All | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Severity | Major | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Time To Fix | Instant (5 mins) | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Precision | Very high | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Examples | :ref:`case-thelia-php-betterrand`, :ref:`case-fuelcms-php-betterrand` | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Available in | `Entreprise Edition `_, `Community Edition `_, `Exakat Cloud `_ | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+