How to get the server IP address accurately _php tips

Source: Internet
Author: User

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.

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.