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");
}