Skip to content
PHP Logo

Are there best practices for handling permissions and credentials when executing system commands, such as shutdown, in PHP scripts?

When executing system commands like shutdown in PHP scripts, it is important to handle permissions and credentials securely to prevent unauthorized access or misuse. One best practice is to use sudo to execute the command with elevated privileges, ensuring that only authorized users can perform the action. Additionally, you can use PHP's shell_exec function to execute the command securely within your script.

php
<?php
// Execute shutdown command with sudo
$command = 'sudo shutdown -h now';
$output = shell_exec($command);

echo $output;
?>