Invalid string length $ _SERVER ['SERVER_NAME']

So, I’m trying to check if the Apache server name contains a certain string, and noticed a very strange behavior (for example, no matches in the if statement, even if the lines turned out to be exactly the same). 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 contains only 11 characters if you do not count the numbers. If I declare a variable with 'test.local:5757'instead $_SERVER['SERVER_NAME'], I get the correct length, 15.

I tried adding an empty line to the end of the "reset" line, I even tried adding extra letters to the line, which eventually counted, but 5757 is still not counted.

Has anyone ever experienced this?

Edit: Sorry, it happened after posting that I did not add enough information.

One of the main details that I forgot is that I use CodeKit on top of MAMP. My local MAMP installation is in localhost, and my code project is CodeKit test.local:5757'. However, it looks like port 5757 is displayed when I repeat the variable declared as $_SERVER['SERVER_NAME']. Even stranger is the fact that the echo $_SERVER['SERVER_NAME'] . ':' $_SERVER['SERVER_PORT']prints test.local:5757, when you change the colon to any other character, for example $_SERVER['SERVER_NAME'] . '>' $_SERVER['SERVER_PORT'], prints test.local:5757>80.

Here are some more examples of what I see:

$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

Sorry for not being marked as related to CodeKit. Any help is much appreciated!

+4
source share
1 answer

https://github.com/bdkjones/CodeKit/issues/440,

, → MAMP Host.

test.local localhost, :

if (strpos($_SERVER['HOST_NAME'],'localhost') !== false) {
    echo 'localhost';
} else {
    echo 'not localhost';
}
+1

Source: https://habr.com/ru/post/1605848/


All Articles