What are the potential pitfalls of using shell_exec in PHP for executing commands and handling output?
Potential pitfalls of using shell_exec in PHP include security vulnerabilities, as it allows for the execution of arbitrary commands on the server. To mitigate this risk, it is recommended to validate and sanitize user input before passing it to shell_exec.
$command = $_POST['command'];
// Validate and sanitize user input
if (preg_match('/^[a-zA-Z0-9\s]+$/', $command)) {
$output = shell_exec($command);
echo $output;
} else {
echo "Invalid command";
}