What security risks are associated with using shell_exec() in PHP scripts?

Using shell_exec() in PHP scripts can pose security risks such as command injection attacks if user input is not properly sanitized. To mitigate this risk, it is important to validate and sanitize any user input before passing it to shell_exec(). Additionally, using escapeshellarg() or escapeshellcmd() functions can help prevent command injection vulnerabilities.

$user_input = $_POST['user_input']; // Example of user input

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

// Execute shell command with sanitized input
$output = shell_exec("ls " . $sanitized_input);