What are some alternative methods for securely storing sensitive data, such as FTP and MySQL credentials, in PHP scripts?

Storing sensitive data, such as FTP and MySQL credentials, directly in PHP scripts can pose a security risk as the information can be easily accessed if the script is compromised. To securely store this data, one alternative method is to use environment variables. By setting environment variables on the server and accessing them in the PHP script, the sensitive data remains hidden from prying eyes.

// Set environment variables on the server
// For example, in Apache, you can use SetEnv directive in the virtual host configuration

// Access the environment variables in PHP script
$ftpUsername = getenv('FTP_USERNAME');
$ftpPassword = getenv('FTP_PASSWORD');

$mysqlHost = getenv('MYSQL_HOST');
$mysqlUsername = getenv('MYSQL_USERNAME');
$mysqlPassword = getenv('MYSQL_PASSWORD');

// Use the credentials in your PHP script
$ftpConnection = ftp_connect($ftpHost);
ftp_login($ftpConnection, $ftpUsername, $ftpPassword);

$mysqli = new mysqli($mysqlHost, $mysqlUsername, $mysqlPassword);