问题
When it comes to programming your web application in php, what is the most efficient way to prevent your MySQL information from being disclosed or discovered by another person (third party)? The information would include domain names and log in and passwords used in the connet functions.
For an example a good procedure might be keeping your mysql connection functions in a separate php file, etc.
Any ideas?
回答1:
- Keep your credentials in a separate
.php
file outside the document root (so it is not directly accessible over the web) - It is better to keep it a
.php
file instead of, say,.inc
, so that even if it is accidentally accessible over the web, it will be executed and not displayed directly - Do not keep the username and password after you have established the connection (i.e., unset the vars or array keys holding the credentials after you don't need them anymore); you cannot accidentally expose what you don't have anymore
- Do not allow repeated inclusion of the credentials file (e.g.
if (defined('DB_AUTH_LOADED')) return; define('DB_AUTH_LOADED', 1);
in your credentials file), in order to avoid any possible redefinition of your credentials vars
This should protect you from direct access to your credentials and from accidental leaks of the credentials by your own code. If attackers can upload PHP files to your server and manage to actually execute them, the fight is pretty much lost, but the above measures should keep you fairly safe from accidentally revealing your creds yourself.
回答2:
I cannot say if this is the best, but this is how I do it;
I have a config.php file, which holds my DB connection credentials, and is not directly accessible. I have a DB class which requires the config.php to make the connection, and I have a site class which extends the DB class.
This is a very common way of doing things, and is generally accepted.
回答3:
Another thing you can do (should your credentials be compromised) is to make sure that the mysql user you set up for tasks which PHP will be using is barred from making DROP statements and maybe even DELETE statements if you can structure your data so.
Give that user the minimum level of access to other databases possible, probably only the one that holds that users data.
回答4:
Classes, includes? Plenty of ways to do it but ultimately if someone has access to the unparsed PHP files, they'll have your MySQL login details.
来源:https://stackoverflow.com/questions/12492192/what-is-the-best-way-to-keep-your-mysql-credentials-private-in-php