PHP dump $_REQUEST to file
Dumping the contents of $_REQUEST directly to a file is a common debugging tactic when you suspect data corruption or need to inspect raw input without modifying application logic. However, because $_REQUEST is a super-array that merges $_GET, $_POST, and $_COOKIE, blindly serializing it can lead to misleading output where the source of the variable is obscured. The approach you choose depends on whether you are running PHP as an Apache module with access to the filesystem or if you need a server-side solution that works regardless of your web server configuration.
Using var_export for Readability
The most straightforward method involves leveraging PHP's built-in var_export() function within a custom error handler or a specific script entry point. This function returns a string containing a representation of the variable, which is generally safer and more readable than print_r() because it produces valid PHP code that can be parsed back into an array later if needed.
To implement this, you can create a lightweight middleware or filter that intercepts the request before the main application logic runs. If you are working on a legacy project where adding a framework is not an option, you might inject this logic at the very top of your entry file (e.g., index.php):
<?php
// Only run if no specific action is requested to avoid dumping on every page load in production
if (!isset($_GET['dump']) && !defined('APP_DEBUG')) {
exit;
}
$requestDump = var_export($_REQUEST, true);
file_put_contents('/var/www/html/debug/request_' . date('Y-m-d_His') . '.php', $requestDump);
die('Request dumped.');
In this pattern, we use date() to ensure filename uniqueness, preventing race conditions where two simultaneous requests might overwrite each other's debug logs. We also guard against accidental execution on every page load by checking for a specific query parameter or a debug constant. The resulting file contains a PHP array representation that is easy to read in any text editor and can be re-imported into your script using include or require for deeper inspection.
Handling Filesystem Permissions
A frequent pitfall when writing to disk is ignoring filesystem permissions. If the web server user (typically www-data, apache, or nobody) does not have write access to the target directory, the application will throw a "Permission denied" error, breaking the script execution flow.
Before implementing any logging strategy, verify that your log directory exists and is writable by the web server process. On Linux systems, you can check this with:
ls -ld /var/www/html/logs
# Ensure the group or owner matches the web server user
chown www-data:www-data /var/www/html/logs
chmod 755 /var/www/html/logs
If you cannot modify directory permissions due to security policies, consider using a symlink to a location where the web server has write access, such as /tmp, though be aware that files in /tmp are often cleared on reboot.
Framework-Specific Approaches
If you are working within a framework like Laravel, manually handling $_REQUEST is often redundant because the framework already exposes these superglobals via its request object. In a Laravel environment, accessing request()->all() provides the same data but with better type hinting and separation of concerns.
However, if your goal is strictly to persist this data for external analysis without altering the application's routing or controller logic, you can register a console command or create a custom kernel bootstrapper hook that triggers only when a specific environment flag is set. For instance, in Laravel, you might add a check at the start of your bootstrap phase:
// In app/Console/Kernel.php or a dedicated service provider
if (env('REQUEST_DUMP_ENABLED') === true) {
$dump = var_export(request()->all(), true);
file_put_contents(storage_path('app/request_dump_' . date('Y-m-d_His') . '.txt'), $dump);
}
This approach keeps the debug logic isolated and ensures it only runs when explicitly enabled, preventing accidental data leakage in production environments.
Security Implications
Finally, remember that dumping sensitive variables to a file is a security risk. $_REQUEST can contain session IDs, CSRF tokens, authentication credentials, or API keys depending on how your application is structured. If an attacker triggers this dump mechanism intentionally, they could exfiltrate sensitive data by accessing the log files directly via HTTP.
Always restrict access to these debug files using filesystem permissions (read-only for the web server user) and ensure that any file path used is outside the public directory hierarchy. Never expose these files through a URL without strict authentication checks.