PHP accurately obtains the server IP address. php obtains the server ip_PHP tutorial

Source: Internet
Author: User
PHP accurately obtains the server ip address, and php obtains the server ip address. PHP accurately obtains the server ip address. php obtains the server ip address. This article describes how PHP accurately obtains the server ip address. Share it with you for your reference. The specific analysis is as follows: PHP accurately obtains the server ip address, and php obtains the server ip address

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.

Examples in this article describes how PHP can accurately obtain server IP addresses. Share it with you for your reference. The specific analysis is as follows :...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.