treehouse : what would you like to learn today?
Web Design Web Development iOS Development

development and production websites confusion

  • I'm new to having a local copy of a website with MAMP and uploading any changes to the production site (using Coda).
    I'm struggling on how I prevent specific local settings (DB, local path etc.) in my PHP files overwriting the production server settings and crashing it?
    I'd rather not have to publish a config file to the production site, then go have to update all the settings again for the production environment, but can only guess I have to do it?
  • Use relative paths everywhere would be my suggestion. I avoid absolute paths as much as possible, so instead of pointing to an image like this:

    <img src="http://localhost/images/party.jpg"&gt;
    I will do something like this:

    <img src="/images/party.jpg">
    If you don't use any of that anywhere (when calling your CSS, Javascripts, etc.) the both local and production won't get in eachother's way:

    <script type="text/javascript" src="/js/scriptstuff.js">
    When pointing to the database, always point to "localhost", so no matter what environment you're working on, it will always use the database on the same server where your files are.

    Hope that makes sense!
  • Thanks. Yes I had been using the relative paths for the images, which was lucky but I've found that I have some problems with the $_SERVER['DOCUMENT_ROOT'], that I use to reference some resources outside of the web root, being unavoidably very different on both locations.

    It was hardcoded when I worked only remotely on the site, but now I find I need to set it for the development site in my config file, then upload to production, then re-edit remotely to fix it for the new server.
  • are you using Wordpress?
  • No, bespoke sites.
  • Personally I keep all of my global domain variables in a separate file called site.config. This holds all of the PHP constants that change between local server and my hosting.

    Then you just don't upload it.
  • Great, thanks guys. I like to keep my sites as 'portable' as possible, so as they could be picked up from one host and dropped onto another with very little fuss/downtime etc. I've spent years just working on them 'live', but trying to take a more considered approach now with development/production stages. :D