Every projects have some kind of configuration such as database username, database password, database name blah blah no matter whats language it’s been written in.
But i will be primarily talking about php here.
In past what I used to change the config file every time I had to push updates into the live server either by FTP or SFTP or VCS(Version Control System) and that was painful. But thanks to the internet I found some tricks somewhere in the internet that I dont remember right now which I have been using since then which allows me to use the same config file both in localhost and in live server.
Lets say we have the following config values which are different when the code is running in Localhost and in server. Say for localhost they have the following values
1 2 3 4 5
And in server the value is different as follows
1 2 3 4 5
Now as I have already said changing those before every push is a pain in the a**. The way we can have them both at the same time and both environment runs properly is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
FYI, $_SERVER[‘REMOTE_ADDR’] returns users IP address if the user is not using Proxy.
If you are using Virtual host then you can use $_SERVER[‘SERVER_NAME’]. Suppose I have a virtual host setup for all my office projects as d.noobis.com then we can use the following conditionals to determine the correct environment
1 2 3 4 5 6 7 8 9 10 11 12 13
Using this we can remove the burden of editing config file before every commit/upload of codebase into live server. This same technique can be used for frameworks which uses pure php variables for configuration options like Code Igniter.
But if your framework uses special formats for config files like YML then there might be different ways to separate development and production environment settings.
Hope you enjoyed the post. :)
As Zaman-A-Piri-Pasa suggested in the comment, if you are on windows using XAMPP use
instead to check for localhost.