Skip to content
PHP Logo

Is storing database access credentials in a PHP script secure, or is using .htaccess recommended?

Storing database access credentials in a PHP script is not secure because the credentials can be easily exposed if the PHP script is compromised. Using .htaccess to protect sensitive files, including the file containing the credentials, is recommended as it adds an additional layer of security by restricting access to the file from unauthorized users.

php
// Example of using .htaccess to protect sensitive files
// Create a .htaccess file in the directory where your PHP script containing the credentials is located
// Add the following code to the .htaccess file to restrict access to the file
<Files "credentials.php">
    Order Allow,Deny
    Deny from all
</Files>