Remote or local server?

Hello!

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.

Any thoughts would be appreciated.

Thank you,

Eric

I’m not sure which value works but do


foreach($_SERVER as $key => $value) {
   echo "$key is set to $value<br/>";
}

You can then see which key for $_SERVER to test for =D

3 ways:

  1. Create a file called c:/flag.php on localhost and then check for it existence using file_exists();

  2. If your localhost is Win32 and the live server is *nix, check the value of the constant PHP_OS

  3. Use something like


if (isset($_SERVER["COMPUTERNAME"]) && $_SERVER["COMPUTERNAME"] == 'YOUR_COMUPTER_NAME') {
    $inifile = APPLICATION_PATH . '/config/YOU_COMPUTER_NAME_settings.ini';
}

Thank you both! (I especially like the slick method of Cups #1).

-Eric

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
}

I just use $_SERVER[‘SERVER_NAME’] with a switch statement. The default: case is the live server, ad my local machines have their own values.

Thanks, I saw it on here years ago.

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.

How would you do this on win servers?

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.