I believe that many people will comment and uncomment blocks (or individual lines) of code when switching between local and live running of their pages. This can be a pain. I use the following code and just change a value from true to false when moving to the remote/live site:
PHP Code:
<?php if (!defined('LOCAL')) { define('LOCAL', true); // set this to true or false } if (!defined('DB_USER')) { if (LOCAL) { DEFINE('DB_USER', 'Andrew'); DEFINE('DB_PASSWORD', 'Password1'); DEFINE('DB_HOST', 'localhost'); DEFINE('DB_NAME', 'AndyDB'); DEFINE('EMAIL', 'andy@somemail.com'); DEFINE('BASE_URL', 'http://localhost:80/AndysProject/'); } else { DEFINE('DB_USER', 'user2'); DEFINE('DB_PASSWORD', 'Password2'); DEFINE('DB_HOST', 'www.some.com'); DEFINE('DB_NAME', 'db2'); DEFINE('EMAIL', 'host@someother.com'); DEFINE('BASE_URL', 'http://andyshost.com/'); } } ?>
I store this in a config.php file that is included in relevant pages.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 12-17-2012 at 01:37 AM..
<VirtualHost 10.0.0.253:80>
DocumentRoot "/Path/To/Folder"
ServerName helloworld.local
# This should be omitted in the production environment
# Or set to 'production'
SetEnv APPLICATION_ENV development
<Directory "/Path/To/Folder">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This way, you can configure a whole host of different application environments. Useful for multiple developers all with their own way of setting up their DB's, passwords, paths, whatever etc.
Is APPLICATION_ENV specific to certain frameworks or servers? But I suppose it can be assigned in any comparable settings file.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 12-18-2012 at 12:28 AM..
Its not specific to any particular framework or server. (I suppose you would have to have apache and use virtualhosting)
Whats really cool about this system, is you can chain settings. EG:
Developer
setting_1 = x
setting_2 = y
setting_3 = z
Developer : Developer_1
setting_3 = a
setting_4 = i
So setting something like, APPLICATION_ENV = Developer_1, you can then chain settings and use inheritance. Like css I suppose. Of course, you have to write the logic for all that.
Its also worth taking a look at how big frameworks etc handle their settings. I really like the zend framework approach, which is why I hacked off its setting system.
Use the $_SERVER['HTTP_HOST'] to determine which system your script is running on. That way you don't even need to set anything to true or false, no constants to define etc.
Just upload all your files (including your mysql config file with no changes for once) and it'll just work straight away. It supports various names / domains for each server (handy if you have subdomains) and can be adjusted for mysqli if needed:
PHP Code:
//Require the file require_once('mysql.php');
//Open connection and select DB $MySQL = run_mysql($_SERVER['HTTP_HOST']);
//Or run_mysql($_SERVER['HTTP_HOST']);
//Need to debug this mysql connection? - Set the second parameter to true. run_mysql($_SERVER['HTTP_HOST'], true);
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
You're right, it's better - no need to tell it what server your on as it picks the correct SQL credentials automatically
No need to manually adjust a define statement, no need to change anything really, just upload everything and you're done (You can probably tell I like automation lol)
The only thing mine won't do is work with cron (for obvious reasons being that there is no domain supplied by a http header). That should be relatively easy to overcome though - you could change the script to work from a machine name instead in the $_SERVER array.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.