I'm currently in the process of making a website using PHP and mysql for the database. I'm pretty much juggling between a local server with WAMP for debugging and sending to the live server. So I was wondering if there was a way to automatically adjust the database name, the username and the password according to the server the website is on. Currently, I'm just doing it manually but I keep forgetting about it and it becomes annoying especially since I'm uploading to the live server several times a day for my friends to test. I've tried using $_SERVER['REMOTE_ADDR']
but for some reason, it doesn't work. Do you guys have any solution or should I just continue doing it manually?
There's several ways to go about it.
If your code allows for environment files such as .env
, use that.
Create a file, say /doc/root/path/.env
with server settings. Get mysql credential from it and don't update it with the rest code.
.env
for your local server and .env
for a production one.
MYSQL_USER=user
MYSQL_PASSWD=passwd
MYSQL_DB=dbname
and
<?php
$config = parse_ini_file('./.env');
Have an environment class which contains the two states, namely production and development.. For each state enter the different database credentials.. In my case I have a constant in the environment class which states whether or not I am in production.. The constant is a boolean value true or false. Eg
Environment::PRODUCTION
The production constant will be boolean..
If in production, it uses the production credentials else uses development, vice versa. You can store the credentials in a .ini file or php file. If it is the former, then make sure the file is outside document root