This article mainly introduces how PHP can accurately obtain the Server IP address, which can be used to skip the proxy to directly obtain the IP address. The example analyzes the related skills of php to obtain the Server IP address. For more information, see
This article mainly introduces how PHP can accurately obtain the Server IP address, which can be used to skip the proxy to directly obtain the IP address. The example analyzes the related skills of php to obtain the Server IP address. For more information, see
This article describes how PHP can accurately obtain the Server IP address. Share it with you for your reference. The specific analysis is as follows:
In php, we generally use $ _ SERVER ['HTTP _ host'] to obtain the domain name or IP address of the website in the URL.
The explanation in the php manual is as follows:
"HTTP_HOST"
Host of the current request: header information.
In general, this will not cause any problems. In some common php frameworks, such as PFC3 and FLEA are also based on this predefined variable.
However, when a program is handed over to the customer for testing in a recent project, it turns out that the redirection of the program always fails.
Finally, find out the cause: $ _ SERVER ['HTTP _ host'] in the customer's environment, the obtained value is always the ip value of the SERVER where the program is located in its LAN.
The reason is that the customer's company connects to the Internet through a server, and the server where our program is located is mapped out through the domain name, that is, there 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 where the program is located in its LAN.
Finally, I checked a lot of information and found an alternative implementation method in the symfony framework:
Set
The Code is as follows:
$ Host = $ _ SERVER ['HTTP _ host'];
Replace:
The Code is 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 php programming.