What are some common pitfalls to avoid when using shell_exec in PHP to run commands on a server?

One common pitfall to avoid when using shell_exec in PHP is not properly sanitizing user input. This can lead to command injection attacks where malicious users can execute arbitrary commands on the server. To prevent this, always validate and sanitize user input before passing it to shell_exec.

$user_input = $_POST['input']; // Assuming user input is coming from a form

// Sanitize user input using escapeshellarg function
$sanitized_input = escapeshellarg($user_input);

// Run the command using the sanitized input
$output = shell_exec("your_command_here " . $sanitized_input);

// Display the output
echo $output;