random_compat is a PHP polyfill that provides cryptographically secure random number generation functions for PHP 5.x projects.
The library solves the problem of generating cryptographically secure random data in older PHP versions that lack the random_bytes() and random_int() functions introduced in PHP 7. It exposes these two CSPRNG functions with identical behavior to their native PHP 7 counterparts, allowing developers to write code that works across both PHP 5 and PHP 7 environments. The polyfill uses the operating system's random data sources where available and will throw an exception rather than fall back to insecure randomness if it cannot safely generate data.
Projects maintaining PHP 5 compatibility while needing secure random generation are the primary use case. This includes libraries and frameworks that must support older PHP versions or applications not yet upgraded to PHP 7. The tool is designed as a drop-in replacement for the native functions, so code written against it will work identically when eventually run on PHP 7 without modification. Installation is available through Composer or as a signed PHP Archive, with manual installation also supported.
Development activity shows a mature, security-conscious approach. The project explicitly recommends using stable releases rather than the master branch and emphasizes that the library has been examined by security experts while acknowledging the possibility of overlooked issues. The tool prioritizes fail-safe behavior by refusing to fall back to insecure randomness under any circumstances, instead throwing an exception to force developers to address the underlying problem. Documentation includes detailed guidance on exception handling differences between PHP 5 and PHP 7, reflecting attention to real-world compatibility concerns.