how detect local or remote server?

Problems with the Windows version of XAMPP, questions, comments, and anything related.

how detect local or remote server?

Postby tcloud » 26. August 2013 12:59

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'];
?>
tcloud
 
Posts: 17
Joined: 05. January 2012 15:14
Operating System: windows 7 64

Re: how detect local or remote server?

Postby Altrea » 26. August 2013 15:52

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())
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: how detect local or remote server?

Postby tcloud » 27. August 2013 15:13

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
tcloud
 
Posts: 17
Joined: 05. January 2012 15:14
Operating System: windows 7 64

Re: how detect local or remote server?

Postby Altrea » 27. August 2013 15:36

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
}
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: how detect local or remote server?

Postby tcloud » 27. August 2013 16:08

thanks ... now I'll have learn what that means ;-)
tcloud
 
Posts: 17
Joined: 05. January 2012 15:14
Operating System: windows 7 64

Re: how detect local or remote server?

Postby Altrea » 27. August 2013 19:20

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
}
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: how detect local or remote server?

Postby tcloud » 27. August 2013 20:33

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).
tcloud
 
Posts: 17
Joined: 05. January 2012 15:14
Operating System: windows 7 64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 111 guests