What are common pitfalls when using exec()/shell_exec() in PHP scripts?

Common pitfalls when using exec()/shell_exec() in PHP scripts include potential security vulnerabilities such as command injection attacks if user input is not properly sanitized. To mitigate this risk, always validate and sanitize user input before passing it to exec()/shell_exec(). Additionally, avoid using these functions with dynamic or unsanitized input to prevent unintended execution of malicious commands.

$user_input = $_POST['user_input'];

// Validate and sanitize user input before passing it to exec()/shell_exec()
$validated_input = escapeshellarg($user_input);

// Execute the command safely
$output = shell_exec("ls " . $validated_input);
echo $output;