How to accurately obtain the server IP address with PHP_PHP Tutorial

WBOY
Release: 2016-07-13 09:52:14
Original
852 people have browsed it

How to accurately obtain the server IP address with PHP

The example in this article describes how to accurately obtain the server IP address with PHP. Share it with everyone for your reference. The specific analysis is as follows:

In php, we usually 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”

The content of the Host: header information of the current request.

Generally speaking, you will not encounter any problems in this way. Some common PHP frameworks, such as PFC3 and FLEA, are also based on this predefined variable.

However, in a project I was working on recently, when the program was handed over to the customer for testing, I found that the jump of the program always went wrong.

Finally find out the reason: In the customer's environment, the value obtained by $_SERVER['HTTP_HOST'] is always the IP value of the server where the program is located in its LAN.

The reason is that the customer's company is connected to the Internet through a server, and the server where our program is located is mapped through the domain name, that is, there is a "proxy" process in the middle.

Therefore, in such an environment, the value obtained by $_SERVER['HTTP_HOST'] 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:

Will

The code is as follows:

$host = $_SERVER['HTTP_HOST'];

Replace with:

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 be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1009817.htmlTechArticleHow to accurately obtain the server IP address with PHP. This article describes how PHP can accurately obtain the server IP address. Share it with everyone for your reference. The specific analysis is as follows: In php, we...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template