Page 1 of 1

how detect local or remote server?

PostPosted: 26. August 2013 12:59
by tcloud
I want to automatically detect the current environment for my scripts -- whether local or remote server. I currently use the code below and would appreciate comments on whether there is a better way or if I'm missing something.

Code: Select all
<?php
// determine if running on local or remote (weblive)
//
// XP --> 'Documents and Settings
// Vista, Win7, Win8 --> C:/Users _OR_ C:\Users';
// IIS --> inetpub
// detect MAMP, WAMP or XAMPP installs
// Apple OS X --> /Library/WebServer
//
$local_keys = array(
   'Documents and Settings',
   ':\Users\\',
   ':/Users/',
   ':\Windows',
   ':/Windows/',
   ':\xampp\\',
   ':/xampp/',
   ':\wamp\\',
   ':/wamp/',
   ':\mamp\\',
   ':/mamp/',
   ':\inetpub\\',
   ':/inetpub/',
   '/Library/WebServer/'
);
//
$weblive = true;
$hoststr = $_SERVER['SCRIPT_FILENAME'];
foreach($local_keys as $key) if(stristr($hoststr, $key)) $weblive = false;
//
if ($_SERVER['HTTP_HOST'] == 'localhost' && !$weblive) $hostname = '127.0.0.1';
else $hostname = $_SERVER['HTTP_HOST'];
?>

Re: how detect local or remote server?

PostPosted: 26. August 2013 15:52
by Altrea
That depends how your own behavior with development / test environments is.
Often something like this is enough:
Code: Select all
if('127.0.0.1' == $_SERVER["REMOTE_ADDR"])
{
    // set local config parameters
}


You can add local addresses too or build an array of addresses and check with in_array()

Another common possibility would be to set an Apache environment variable (SetEnv) for example in an .htaccess file and read this out with php (getenv())

Re: how detect local or remote server?

PostPosted: 27. August 2013 15:13
by tcloud
That wouldn't work for my setup? -- see below:

[SERVER_SOFTWARE] => Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
[SERVER_NAME] => localhost
[SERVER_ADDR] => ::1
[SERVER_PORT] => 80
[REMOTE_ADDR] => ::1
[DOCUMENT_ROOT] => C:/xampp/htdocs

Re: how detect local or remote server?

PostPosted: 27. August 2013 15:36
by Altrea
Okay, so you are using IPv6 too.

How about that:

Code: Select all
if(in_array($_SERVER["REMOTE_ADDR"], array('127.0.0.1', '::1')))
{
    // set local config parameters
}

Re: how detect local or remote server?

PostPosted: 27. August 2013 16:08
by tcloud
thanks ... now I'll have learn what that means ;-)

Re: how detect local or remote server?

PostPosted: 27. August 2013 19:20
by Altrea
Thats not that difficult.
You know all the constructs already maybe except the php function in_array().
in_array simply checks if a value ($_SERVER['REMOTE_ADDR']) is part of an array (127.0.0.1 and ::1 are building this array).
And ::1 is simply the same as 127.0.0.1 just for IPv6.

You can use something like this too if you feel more comfortible with:
Code: Select all
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1' or $_SERVER['REMOTE_ADDR'] == '::1') {
    // set local configuration parameters
}

Re: how detect local or remote server?

PostPosted: 27. August 2013 20:33
by tcloud
thank you for taking the time to respond. I don't have a problem with the PHP, it's the "::1" that I wasn't familiar with. I looked it up and found that it is IPv6 loopback address and is equivalent to 0:0:0:0:0:0:0:1 (which is what you wrote, but I didn't understand it).