What are the potential security risks associated with using exec() in PHP for executing shell commands?

Using exec() in PHP for executing shell commands can pose security risks such as command injection attacks if user input is not properly sanitized. To mitigate this risk, it is essential to validate and sanitize any user input before passing it to the exec() function.

$user_input = $_POST['input'];

// Sanitize user input to prevent command injection
$clean_input = escapeshellarg($user_input);

// Execute the sanitized command
exec("command $clean_input");