3

I would like to detect an array of strings in $_SERVER["SERVER_NAME"] to define a constant for environment like dev/prod.
Basically, if localhost or .dev is in the URL, that would set the constant to "prod".

Here is my try but I always got "prod" even if my current url is "localhost:3000" or "site.dev":

// Define environment
$dev_urls = array(".dev", "localhost");
if (str_ireplace($dev_urls, "", $_SERVER["SERVER_NAME"]) == $_SERVER["SERVER_NAME"]){
    define("ENV", "dev");
} else {
    define("ENV", "prod");
}

Finally used this code which works like a charm

// Define environment
$dev_urls = array(".dev", "localhost", "beta.");
if (str_ireplace($dev_urls, "", $_SERVER["HTTP_HOST"]) != $_SERVER["HTTP_HOST"]){
    define("ENV", "dev");
} else {
    define("ENV", "prod");
}
flks
  • 610
  • 10
  • 28
  • I never received any feedback regarding the answer I left; did it solve your problem? If it did, marking it as accepted would be appreciated; thanks. – miken32 Apr 23 '16 at 17:01

2 Answers2

3

The SERVER_NAME is defined in the server config, it never changes no matter what URL you use to reach that page.

I believe you want to use HTTP_HOST instead.

if ($_SERVER['HTTP_HOST'] == 'localhost' || substr($_SERVER['HTTP_HOST'], -4) == '.dev')
    define('ENV', 'dev');
else
    define('ENV', 'prod');
Havenard
  • 27,022
  • 5
  • 36
  • 62
  • Agree, but it doesn't seems to solve the issue, always got "prod". – flks May 03 '15 at 20:32
  • @flks Because your hacky `if` statement doesn't make sense. Just check `if ($_SERVER['HTTP_HOST'] == 'localhost')` to determine if you are in dev enviroment. – Havenard May 03 '15 at 20:34
  • The fact is I can access the dev URL as localhost:3000 **or** site.dev – flks May 03 '15 at 20:35
  • Yep it works! But the challenge was to use an array to read and change it easily. The question is more how can I detect an array of strings in this `$_SERVER['HTTP_HOST']`? – flks May 03 '15 at 20:39
  • `(stristr($_SERVER['HTTP_HOST'], ['localhost', '.dev']) !== false)` is true if any of those words exist on the string. – Havenard May 03 '15 at 20:41
  • But it doesn't seem a fit way of testing it because both `www.localhost.net` and `www.deviantart.net` for instance will match your criteria, and thats not what you are looking for. – Havenard May 03 '15 at 20:47
0

You can use array_filter() and an anonymous function to do what you're looking for.

$dev_urls = [".dev", "localhost"];
$host = $_SERVER["HTTP_HOST"];
$result = array_filter($dev_urls, function($e) use ($host) {
    return strpos($host, $e) !== false;
});
if ($result) {
    //development URL
} else {
    //production URL
}

However, this is a substring search, which is potentially inaccurate. I'd suggest doing a full match instead.

$dev_urls = ["foo.dev", "localhost"];
$host = $_SERVER["HTTP_HOST"];
$result = array_filter($dev_urls, function($e) use ($host) {
    return $host === $e;
});
if ($result) {
    //development URL
} else {
    //production URL
}
miken32
  • 42,008
  • 16
  • 111
  • 154