What are the best practices for securely storing login credentials in a PHP script for automated tasks?

Storing login credentials securely in a PHP script for automated tasks is crucial to prevent unauthorized access to sensitive information. One common approach is to store the credentials in a separate configuration file outside of the web root directory and restrict access to it. Another method is to encrypt the credentials before storing them and decrypt them when needed in the script.

<?php
// Define the login credentials
$username = 'your_username';
$password = 'your_password';

// Encrypt the credentials
$encrypted_username = base64_encode($username);
$encrypted_password = base64_encode($password);

// Store the encrypted credentials in a separate configuration file
file_put_contents('config.php', "<?php define('USERNAME', '$encrypted_username'); define('PASSWORD', '$encrypted_password'); ?>");

// To use the credentials in your script, include the configuration file
include 'config.php';
$decrypted_username = base64_decode(USERNAME);
$decrypted_password = base64_decode(PASSWORD);

// Use the decrypted credentials to authenticate
// Your code here
?>