Skip to content
PHP Logo

What are the recommended methods for handling login credentials securely in PHP scripts that access a MySQL database for email distribution purposes?

To handle login credentials securely in PHP scripts that access a MySQL database for email distribution purposes, it is recommended to store the credentials in a separate configuration file outside of the web root directory. This helps prevent direct access to the credentials by unauthorized users. Additionally, using PHP's built-in functions like password_hash() and password_verify() can help securely store and verify passwords.

php
<?php
// config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');

// email_distribution.php
require_once('config.php');

// Connect to MySQL database
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

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