One solution is using environment variables to your application. You'd store your credentials in environment variables of your system and then in your application you read the value of the environment variables to make your database connection (you can also use some kind of encryption). So, basically, you set up the following variables as environment variables:
- DB_USER
- DB_PASS
- Others
Then in your code you use getenv(VAR_NAME)
to make the database connection. So, in your case, would be something like this:
$connectionInfo = array("Database"=>"saz09", "UID"=>getenv("DB_USER"), "PWD"=> getenv("DB_PASS", "CharacterSet"=>"UTF-8");
You should also verify if the value of both variables exists before set their value to the datbase connection.
Positive points:
- There's no hard code credentials in your code (if people has access to your code, they wont have the credentials) which makes more secure;
- It's configurable for every environment, in case others people share the same project, you guys configure your own database connection (no risk to use accidentally production connection);
Negative points:
- If someone has access to the server you're hosting your application, they can have access to your env vars.
Edit: As @Chris mentioned, this is not a negative point, since if someone has access to your server, it means your application is in trouble no matter how we store the credentials. Thank you for the clarification, @Chris.
You can also add more security by adding encryption to the string and in your code you decrypt the credentials string.
I'm not sure which OS you use, so here's a short guide to configure environment variables in both most famous OS:
There's a lot of other ways to do an approach similar to this, if you want to get familiarized to them, take a read in the link: