I was wondering if there’s a clean way to test whether my web page is on a local environment (on my computer) or on the web; currently each time I switch back and forth I have to change one of the main configs and it would be great if I could automate it. I thought that I could use $php_self and do a search to see if “localhost” existed in my url, but since it’s the root, it just gives me the stuff afterwards, which isn’t helpful.
Well what I said isn’t a solution, its a way to find a solution heh, I dont have $_SERVER and other globals memorized, instead of just have the foreach memorized cuz I can always find what I want to test for.
So you could in this case test:
if( $_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR'] ) {
// is local
} else {
// is remote
}
The only problem, as I found out - and really should have pointed out - is that when you put that code on the live server every page that needs a bootstrap file ends up with making a wasteful file_exists() call.
Still, its quick and dirty and sometimes that is all you need.
I personally avoid using $_SERVER for this case. My favourite way to do this is to set environmental variables in my virtual host (apache).
<VirtualHost *>
SetEnv ENVIRONMENT development
<Directory>
# ...
</Directory>
</VirutalHost>
switch(getenv('ENVIRONMENT')) {
case('development') :
// do developement specific stuff
break;
case('production') :
// do production specific stuff
break;
default:
// load default config
}
I find this much more flexible then most other solutions because it’s more portable. It’s a god send when working in the cloud because it’s not exactly practical to edit your code and change the value of ‘YOUR_COMUPTER_NAME’, or what you’re using to determine environment, when you’re deploying to multiple servers or spinning up instances on the fly.
If you’re using apache, which I assume you are, just put add the SetEnv line to your virtual host configuration. I think it’s in you the httpd.conf on windows. Otherwise you can add that line to your .htaccess file.
You also have to make sure mod_env is enabled (not sure how to do this on windows)
Here’s a link from StackOverflow that explains some other installation steps.
I know it seems like a lot of work if you’re not familiar with apache but it’s a little thing that can save you big time in the future.
Thanks xzyfer! I will definitely look into that this afternoon, would be great for automatically using my dev DB or the live DB when I’m trying to add a feature or upgrade a feature. Instead of going thru everytime and changing table names whenever I want to make it live.