PHP accurate access to server IP address method, PHP get server ip_php Tutorial

Source: Internet
Author: User

PHP accurate method of obtaining the server IP address, PHP to obtain the server IP


In this paper, we describe the exact method of obtaining the server IP address by PHP. Share to everyone for your reference. The specific analysis is as follows:

In PHP, we generally live by $_server[' Http_host '] to the domain name or IP address of the website in the URL.

The explanations in the PHP manual are as follows:

"Http_host"

Host of the current request: the contents of the header information.

In general, this is not a problem, and in some common PHP frameworks, such as PFC3 and Flea, are based on that predefined variable.

However, recently in a project, the program handed over to the customer in the hands of the test, unexpectedly found that the program jumps 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, is through the domain name Mapping, that 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 in its local area network where the program resides.

Finally, a lot of data, in the Symfony framework, find alternative implementation methods:

Copy the Code code as follows: $host = $_server[' http_host ');
Replace with:
Copy the 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1011249.html www.bkjia.com true http://www.bkjia.com/PHPjc/1011249.html techarticle PHP Accurate access to the server IP address method, PHP obtains the server IP This article describes the exact way that PHP obtains the server IP address. Share to everyone 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.