What are some best practices for creating buttons in PHP that trigger batch files to perform specific actions?

When creating buttons in PHP that trigger batch files to perform specific actions, it is important to ensure proper security measures are in place to prevent unauthorized access to the batch files. One way to achieve this is by using PHP sessions to authenticate users before allowing them to execute the batch files. Additionally, it is recommended to sanitize user input to prevent any malicious code injection.

<?php
session_start();

if(isset($_POST['action'])) {
    if($_SESSION['authenticated'] == true) {
        $action = $_POST['action'];
        
        // Sanitize user input to prevent code injection
        $action = escapeshellarg($action);
        
        // Execute the batch file with the specific action
        exec("path/to/batch/file.bat $action");
    } else {
        echo "Unauthorized access!";
    }
}
?>