Handling Includes across Environments

Discussion in 'PHP' started by DoubleDee, Sep 29, 2013.

  1. #1
    My website is coded using a procedural style.

    I am trying to find a way that I can code my Includes so that when I switch between my Dev and Production environments that I don't have to rewrite a bunch of code.

    The problem is that I have my Config and Database Settings files stored locally in my Web Root for development, but when I upload things to my VPS, I want to create a directory outside of the Web Root for added security.

    Right now most of scripts start off like this...

        // Access Constants.
        require_once('../config/config.inc.php');
    
        // Connect to Database.
        require_once(WEB_ROOT . 'private/mysqli_connect.php');
    
    PHP:
    Once I upload that to my Virtual Server, it won't work if I move my Config and Database files, so I'm not sure how to make it so my code more easily adapts as I switch back and forth?!

    Sincerely,


    Debbie
     
    DoubleDee, Sep 29, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    cakephp do it like this
    
    class DATABASE_CONFIG {
    
        public $default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'myusername',
            'password' => 'mypassword',
            'database' => 'my_database',
            'prefix' => '',
            //'encoding' => 'utf8',
        );
    
        public $test = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'user',
            'password' => 'password',
            'database' => 'test_database_name',
            'prefix' => '',
            //'encoding' => 'utf8',
        );
    }
    PHP:
    but that could just as easily be
    
    $db_config = array(); 
    $db_config['production'] = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'myusername',
            'password' => 'mypassword',
            'database' => 'my_database',
            'prefix' => '',
            //'encoding' => 'utf8',
        );
    
      $db_config['test'] =  = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'user',
            'password' => 'password',
            'database' => 'test_database_name',
            'prefix' => '',
            //'encoding' => 'utf8',
        );
    PHP:
    and then when you connect you just need a quick test to see if you are live or in test. The way I've done that in the past is to getcwd() as it will look quite different depending on the host.
     
    sarahk, Sep 29, 2013 IP
  3. DoubleDee

    DoubleDee Greenhorn

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    First, I'm not using OOP, so that doesn't help.

    More importantly, this is an issue of different directory structures, so you're missing the entire point!!


    Debbie
     
    DoubleDee, Sep 30, 2013 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Or you missed mine.

    if you use getcwd() and it matches a particular string then you know you are in test or production and you can then include the appropriate files.

    Here's some code from 2003 when I had apache installed on my c drive for development and testing.
    
    $online = (substr(getcwd(),0,1) != 'c');
    
    if ($online) { // do this }
    else {// do that }
    
    PHP:
     
    sarahk, Sep 30, 2013 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    My question is this: if you're gonna have the config-files etc. outside of your webroot on the live server, why not just use that approach on dev-server as well? Then you won't need to redo any code, just upload to proper directories.
     
    PoPSiCLe, Sep 30, 2013 IP