phpinfo
phpinfo() Explained: The Complete Developer Guide — Advantages, Drawbacks & Best Practices
The phpinfo() function is one of the most widely recognized tools in the PHP ecosystem. Since its introduction in PHP 3, it has served as the go-to diagnostic function for developers and system administrators alike. Yet despite its ubiquity, many practitioners use it without fully understanding its implications — particularly in production environments where security and performance matter.
This comprehensive guide examines everything you need to know about phpinfo(): how it works, why it remains indispensable for debugging, the serious security risks it introduces, and the modern alternatives that are slowly replacing it.
Key Takeaways
- Diagnostic Power:
phpinfo()outputs a complete snapshot of PHP's configuration, loaded modules, server environment, and version information in a single call. - Security Risk: Exposing
phpinfo()output publicly leaks sensitive server details — paths, environment variables, and extension configurations — making it a frequent target for reconnaissance attacks. - Production Forbidden: The function should never remain active in production. Disable it via
php.inior remove it before deployment. - Modern Alternatives Exist: Tools like
php -i, thephpinfo()replacement libraries (e.g., spatie/php-version), and built-inphpinfo(INFO_GENERAL)flags offer safer, more targeted diagnostics. - Built-In Controls: PHP provides
disable_functionsinphp.inito blockphpinfo()entirely at the server level.
What Is phpinfo() and How Does It Work?
phpinfo() is a built-in PHP function that generates a detailed HTML page displaying the current state of PHP on the running server. When called, it outputs information across multiple sections including:
- PHP Version & Build Information: The exact version string, compilation date, and build system.
- Server Environment: Server software, API type (CGI, FPM, Apache module), and server variables.
- Configuration (php.ini): All active and default values for every directive.
- Loaded Extensions: Every enabled module with its version and configuration.
- Environment Variables: OS-level variables accessible to PHP.
- HTTP Headers & Client Information: Request headers, user agent strings, and connection details.
- License & Credits: PHP's license terms and contributor acknowledgments.
Basic Usage
The simplest invocation requires nothing more than:
<?php
phpinfo();
?>
This produces a fully styled HTML document with all sections. For more control, you can pass a bitmask constant to limit output:
<?php
// Only show general info
phpinfo(INFO_GENERAL);
// Show configuration only
phpinfo(INFO_CONFIGURATION);
// Show module info
phpinfo(INFO_MODULES);
// Show environment variables
phpinfo(INFO_ENVIRONMENT);
// Combine multiple flags
phpinfo(INFO_GENERAL | INFO_CONFIGURATION | INFO_MODULES);
?>
The INFO_* constants (available since PHP 4.2.0) give developers precise control over what gets displayed, which is critical when you need specific data without exposing everything.
The Advantages of phpinfo()
1. Rapid Environment Diagnosis
The single greatest strength of phpinfo() is speed. In under a second, a developer gets a complete picture of the PHP runtime — version, extension availability, memory limits, upload max sizes, time zones, and more. This eliminates the need to query dozens of individual configuration directives separately.
2. Extension Verification
When troubleshooting why a library fails to load or a feature doesn't work, phpinfo() instantly confirms whether the required extension is installed and configured correctly. Checking the "Modules" section reveals extension versions, compile flags, and configuration overrides.
3. Debugging Configuration Discrepancies
Different environments (local, staging, production) often have divergent php.ini settings. phpinfo() shows both the Local Value and Master Value for every directive, making it trivial to spot where a setting was overridden — whether by .htaccess, vhost configuration, or runtime ini_set() calls.
4. Zero Dependencies
Unlike third-party diagnostic tools, phpinfo() is part of the PHP core. It requires no Composer packages, no additional installation, and no configuration. It works out of the box on any PHP deployment.
5. Human-Readable Output
The function generates properly formatted, CSS-styled HTML that is immediately readable in a browser. No parsing tools or command-line fluency is required, making it accessible to developers at every skill level.
6. Server-Level Visibility
phpinfo() reveals information that phpinfo() cannot — server software, SAPI type, compiled-in defaults, and sometimes even server paths. This is invaluable when diagnosing infrastructure-level issues.
The Drawbacks and Risks of phpinfo()
1. Critical Security Vulnerability
The most significant drawback of phpinfo() is that it exposes sensitive server information to anyone who can access the generated page. An exposed phpinfo() URL can reveal:
- Full server and PHP version numbers (enabling targeted exploit searches)
- Real file system paths (useful for path traversal attacks)
- Environment variable values (including API keys, database credentials, and tokens if they appear in
$_ENV) - Extension configurations and compile options
- Server architecture and operating system details
CVE-2018-12882 and similar vulnerabilities have demonstrated how attackers routinely scan for phpinfo.php and similar filenames as a first step in reconnaissance. The Shodan search engine indexes thousands of publicly accessible phpinfo() pages worldwide.
2. Performance Overhead
While phpinfo() itself is lightweight, calling it without flags generates an HTML document that can exceed 100 KB. On high-traffic sites, even an accidentally left-behind phpinfo() page can consume bandwidth and increase server load.
3. Outdated Information Risk
phpinfo() reflects the state of PHP at the time the script runs, but it does not validate whether the configuration is correct — only what it is. A developer might see a setting value without realizing it conflicts with application requirements.
4. False Sense of Completeness
The function does not display everything about the PHP environment. Dynamic extensions loaded at runtime via dl(), certain runtime-only configurations, and some OPcache statistics may not appear in the output, leading developers to overlook critical details.
5. Incompatible with Modern Security Hardening
Security-conscious deployments increasingly restrict or disable phpinfo() through disable_functions in php.ini. Relying on it in hardened environments means either weakening security controls or losing the diagnostic capability.