PHP obtains the real IP address of a user.

Source: Internet
Author: User
: This article describes how to obtain the real IP address of a user using PHP. For more information about PHP tutorials, see. I was asked to pass the interview a few days ago. $_SERVER['SERVER_ADDR']What is the problem with the obtained IP address? It must be "if the user cannot obtain the real IP address through a proxy ". Fortunately, the interviewer did not ask further questions. So how can we get the real IP address of the user?

Analysis

Nowadays, many users do not directly dial the Internet, but access the network through routers or other proxies. If we want to obtain the real IP address of the user client, we cannot directly use$_SERVER['SERVER_ADDR']Because the obtained ip address may be a ** Lan ** ip address such as 192.168.1.99, rather than the user's public ip address.

Fortunately, when our access requests are routed through the public network or the proxy server, the terminal will addX-Forwarded-ForHeader information. we can use this request header to obtain the real IP address of the client.

Code


  

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes how to obtain the real IP address of a user in PHP, including some content, and hope to help those who are interested in PHP tutorials.

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.