PHP accurate method of obtaining the server IP address, PHP to obtain the server IP
In this paper, we describe the exact method of obtaining the server IP address by PHP. Share to everyone for your reference. The specific analysis is as follows:
In PHP, we generally live by $_server[' Http_host '] to the domain name or IP address of the website in the URL.
The explanations in the PHP manual are as follows:
"Http_host"
Host of the current request: the contents of the header information.
In general, this is not a problem, and in some common PHP frameworks, such as PFC3 and Flea, are based on that predefined variable.
However, recently in a project, the program handed over to the customer in the hands of the test, unexpectedly found that the program jumps always error.
Finally find out why: $_server[' Http_host ' in the customer's environment, the value obtained is always the IP value of the server on its local area network where the program resides.
The reason is because, the customer's company through a server to connect to the Internet, and our program is located in the server, is through the domain name Mapping, that is, a "proxy" process in the middle.
Therefore $_server[' http_host ' in such an environment, the obtained value is always the IP value of the server in its local area network where the program resides.
Finally, a lot of data, in the Symfony framework, find alternative implementation methods:
Copy the Code code as follows: $host = $_server[' http_host ');
Replace with:
Copy the code as follows: $host = Isset ($_server[' http_x_forwarded_host ')? $_server[' Http_x_forwarded_host ': (Isset ($_server[' http_host '])? $_server[' Http_host ']: ');
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1011249.html www.bkjia.com true http://www.bkjia.com/PHPjc/1011249.html techarticle PHP Accurate access to the server IP address method, PHP obtains the server IP This article describes the exact way that PHP obtains the server IP address. Share to everyone for your reference. The specific analysis is as follows: ...