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
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.
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
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:
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.