4

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!

nickford
  • 782
  • 6
  • 8
  • 2
    post the exact code you use –  Sep 03 '15 at 21:52
  • 2
    server_name should never include port, so i think your not showing us something here –  Sep 03 '15 at 22:00
  • 1
    Short of an OS bug, a race condition (which is highly implausible given that PHP is single-threaded) or an output handler gone wrong, this appears basically impossible to me. The [var_dump part that handles strings](http://bit.ly/1KNfH6h) prints the length of the string, and then passes the same length to `PHPWRITE`. The macro expands to `php_output_write`, which most likely ends up calling [`php_write_stdout`](https://github.com/php/php-src/blob/master/main/output.c#L90) unless an output handler was set, which `fwrite`s the string *with the same length parameter*. – zneak Sep 03 '15 at 22:07
  • 1
    Yes, how are you getting the server name into your variable exactly? Need the code to answer this one... – JeremyB Sep 03 '15 at 22:30

1 Answers1

1

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';
}
user2182349
  • 9,569
  • 3
  • 29
  • 41
  • Thank you thank you, it looks like the thread you posted describes my problem exactly. – nickford Sep 04 '15 at 04:00
  • For anyone else experience this problem, the issue is that CodeKit overwrites the `HOST_NAME` after PHP runs, so even tho `$_SERVER['HOST_NAME']` was printing a 15 character string, the variable is somehow referencing the MAMP host alias, which is in fact, 11 characters... – nickford Sep 04 '15 at 13:33