What are some common methods for storing database connection details in PHP applications?

Storing database connection details securely in PHP applications is crucial to prevent unauthorized access to sensitive information. One common method is to use environment variables to store database credentials outside of the application code, ensuring they are not exposed in version control or other insecure locations. Another approach is to utilize a configuration file that is included in the application but kept separate from the main codebase. Additionally, using a secure password manager or encryption techniques can further enhance the protection of database connection details.

// Example of using environment variables to store database connection details
$servername = getenv('DB_SERVER');
$username = getenv('DB_USERNAME');
$password = getenv('DB_PASSWORD');
$database = getenv('DB_NAME');

// Establishing a database connection using the retrieved credentials
$conn = new mysqli($servername, $username, $password, $database);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";