What potential security risks are involved in executing shell commands through PHP in a multi-server environment?

Executing shell commands through PHP in a multi-server environment can pose security risks such as command injection attacks, where malicious users can manipulate the input to execute arbitrary commands on the server. To mitigate this risk, it is important to properly sanitize and validate any user input before passing it to the shell command.

// Sanitize and validate user input before executing shell command
$user_input = $_POST['input'];

if (preg_match('/^[a-zA-Z0-9\s]+$/', $user_input)) {
    $safe_input = escapeshellarg($user_input);
    $output = shell_exec("your_command_here $safe_input");
    echo $output;
} else {
    echo "Invalid input";
}