Please excuse a beginner question. Suggestions, links, and further reading are all welcome.
I'm concerned about security on web pages and looking for best-practices. When using PHP to connect to MariaDB/MySQL databases, most recommendations put the database passwords right on the PHP page (or on an included page). Is this safe? Is there a better 'best-practices'?
I've searched docs, Stack Overflow, and the interwebs with keywords like "hash passwords MySQL MariaDB PHP7" etc., but the answers are all about clients logging in to a web page, not PHP interacting directly with MariaDB. The MySQL Docs says the passwords are hashed for storage, but that doesn't help my PHP file. The PHP docs don't provide much helpful info and no real-world examples.
So, my questions:
How high is the risk for an HTTP user to download my source files, and see those passwords? (I realize PHP parses pages, so a typical user won't see the raw code or be able to download PHP — and that security of ssh, PHP, MariaDB, etc., is a separate question.)
I get that I can hash the password, but what good does this do, if the password is right there on the same page? (Or am I missing something?)
Is it better/safer to put the database variables in the file, or to use
include("super-sensitive-info.php")
and put the variables there? Can I (should I) hash or encrypt either that file or the passwords, and still make it usable? Can I (should I) hide this file, e.g..super-sensitive-info.php
), and then use server security to restrict access?And, using special characters has given me trouble, e.g.
$password = "pa$$w@rd";
should look like$password = "pa\$\$w\@rd";
, per typical code-in-quotes practice? Or did I miss the memo that I'm not supposed to use special characters for SQL?
To create a simple example, let's say I have two files, looking something like this. Is there a better way? Or is this it?
super-sensitive-info.php
$user = "username"
$password = "password"
// $password = "pa\$\$w\@rd"; // (e.g. if database pw is "pa$$w@rd"?
$database_name = "database_name"
// I can hash it, but this seems only useful for client logins and such,
// unless I can hash this entire file....
// $hashed_password = password_hash($password, PASSWORD_DEFAULT);
index.php
include("super-sensitive-info.php")
$db = mysqli_connect('localhost',$user,$password,$database_name)
or die('Error connecting to MySQL server: ' . mysqli_connect_error());
$query = "SELECT * FROM episodes";