So I am trying to check if the Apache server name contains a certain string, and noticed very strange behavior (for instance, no matches in an if statement, even when the strings appeared to match exactly). Using var_dump()
I looked at my variable containing the server name, and to my surprise I saw this:
string(11) "test.local:5757"
The string is only 11 characters if you don't count the numbers. If I declare the variable using 'test.local:5757'
instead of $_SERVER['SERVER_NAME']
, I get the correct length, 15.
I've tried appending an empty string onto the end to "reset" the string, I've even tried adding additional letters onto the string, which end up getting counted, but the 5757 is still not counted.
Has anyone ever experienced anything like this before??
Edit: Sorry, it occurred after posting that I didn't include enough information.
One major detail I left out is that I am using CodeKit on top of MAMP. My local MAMP installation is located at localhost
and my CodeKit project's URL is test.local:5757'
. However, it does appear that the 5757 port is showing up when I echo a variable that is declared as $_SERVER['SERVER_NAME']
. Even stranger is that echoing $_SERVER['SERVER_NAME'] . ':' $_SERVER['SERVER_PORT']
prints test.local:5757
, while changing the colon to any other character, for instance $_SERVER['SERVER_NAME'] . '>' $_SERVER['SERVER_PORT']
, prints test.local:5757>80
.
Here are a few more examples of what I'm seeing:
$host = $_SERVER['SERVER_NAME'];
echo $host;
// prints 'test.local:5757'
$host = $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
echo $host;
// prints 'test.local:5757'
$host = $_SERVER['SERVER_NAME'] . '&' . $_SERVER['SERVER_PORT'];
echo $host;
// prints 'test.local:5757&80'
$host = $_SERVER['SERVER_NAME'];
echo $host;
// prints 'test.local:5757'
$bool = false;
if ($host == 'test.local:5757') $bool = true;
echo $bool;
// prints false;
$host = $_SERVER['SERVER_NAME'];
var dump($host);
// prints string(11) test.local:5757
Apologies for not tagging as CodeKit related. Any help is hugely appreciated!
There is a nice explanation here https://github.com/bdkjones/CodeKit/issues/440 which ends with
In short, there's nothing wrong here --> MAMP has the correct Host header.
Since you are trying to distinguish between test.local and localhost, you could use:
if (strpos($_SERVER['HOST_NAME'],'localhost') !== false) {
echo 'localhost';
} else {
echo 'not localhost';
}