This article illustrates how PHP accurately obtains the server IP address. Share to everyone for your reference. The specific analysis is as follows:
In PHP, we generally through the $_server[' Http_host ' to live in the URL of the site's domain name or IP address.
The explanations in the PHP manual are as follows:
"Http_host"
The contents of the Host: header information for the current request.
In general, there are no problems with this, and in some common PHP frameworks, such as PFC3 and Flea are based on the predefined variables.
However, recently in a project, the program handed over to the customer hand test, unexpectedly found that the program's jump 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, through the domain name map out, that is, the middle has a "proxy" process.
So $_server[' http_host ' in such an environment, the value obtained is always the IP value of the server on its local area network where the program resides.
Finally, a lot of data, in the Symfony framework, find alternative implementation methods:
Will
Copy Code code as follows:
$host = $_server[' http_host '];
Replace with:
Copy Code 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 will help you with your PHP program design.