The Illusion of Client-Side PHP Execution
You cannot display phpinfo() output by simply posting a string containing the word "phpinfo" or any similar text via a standard HTTP POST request. The fundamental misunderstanding here is conflating data transmission with code execution. When your browser sends the value phpinfo() in the msg field of your form, it is transmitting raw text characters. It is not sending a command to your server's PHP interpreter; it is just data.
To understand why this fails, you must look at how the PHP engine operates on the server. When a request hits go.php, the web server (like Apache or Nginx) passes the request to the PHP-FPM process. The PHP engine reads the file go.php and compiles it into an opcode. It then executes that compiled bytecode. At no point during this execution cycle does the engine scan the contents of the $_POST['msg'] variable for executable code strings and run them.
If you want to display information about your PHP environment, you must invoke the function directly within the PHP script itself. The mechanism relies on the server executing specific instructions found in the file system, not instructions embedded in incoming request bodies.
Implementing phpinfo() Directly
The correct approach is to generate the output on the server before sending any response back to the client. You need to modify go.php to explicitly call the function and pass the information through the standard output stream.
Here is how go.php should look if you want to see the full system details:
<?php
// Capture the POST data as usual
$message = $_POST['msg'] ?? '';
// Check if we have a message, but ensure phpinfo runs regardless
echo "Message : " . htmlspecialchars($message) . "<br><hr>";
// This is the actual function call that generates the output
phpinfo();
?>
In this example, phpinfo() reads your server's configuration file (php.ini), environment variables, and loaded extensions. It then formats this data into a large HTML table and echoes it directly to the browser. The variable $message is now just text sitting alongside that output; it does not trigger anything special.
Handling Security and Output Formatting
By default, phpinfo() dumps a massive amount of information, including file paths, environment variables, and sometimes sensitive configuration details. Displaying this on every page load can clutter the interface significantly. You might consider using the ini_get function to check if you are in a development environment before dumping the info, or wrapping it in a conditional block.
However, there is a critical security consideration when displaying raw PHP output: Output Escaping.
If you were to echo user input directly into an HTML page without sanitization, you risk Cross-Site Scripting (XSS). While phpinfo() generates its own HTML structure, if you mix user input with the output of phpinfo(), you must ensure the user input is escaped using htmlspecialchars(). This prevents any malicious scripts embedded in the posted "message" from executing.
The Performance and Maintenance Trade-off
Using phpinfo() for debugging is standard practice during development, but it should generally be disabled in production environments. The function generates a large HTML document that requires significant bandwidth to transfer and renders slowly in the browser if the PHP configuration is complex (e.g., many extensions loaded).
A common pitfall is relying on phpinfo() for runtime diagnostics in a live application because it doesn't tell you about errors that occur after the script finishes or memory leaks that happen dynamically. For production monitoring, tools like Xdebug or specific logging configurations are far more effective than dumping static config data.
If your goal is to share PHP details with other developers, consider generating a static text report using phpinfo() output and saving it to a file or sending it via email programmatically, rather than displaying the raw HTML table directly on a public-facing page. This keeps the user interface clean while preserving the diagnostic data for external review.
