Php obtains the visitor's real IP address

Source: Internet
Author: User
At most times, we need to get the real IP address of the user, for example, log record, geographic location, user information, website data analysis, etc, in fact, it is easy to get an IP address $ _ server [& amp; 39; r

At most times, we need to get the real IP address of the user, for example, log record, geographic location, user information, website data analysis, etc, in fact, it is easy to get the IP address $ _ server ['remote _ addr.

  1. // The simplest method
  2. $ Ip = $ _ server ['remote _ addr '];
  3. // As long as the proxy is used, you will not be able to obtain the real IP address. the more detailed method is as follows:
  4. Echo "remote addr:". $ _ server ['remote _ addr ']."
    ";
  5. Echo "x forward:". $ _ server ['http _ x_forwarded_for ']."
    ";
  6. Echo "clien ip:". $ _ server ['http _ client_ip ']."
    ";
  7. // Check an instance.
  8. Function getip (){
  9. $ Ip = $ _ server ['remote _ addr '];
  10. If (! Emptyempty ($ _ server ['http _ client_ip ']) {
  11. $ Ip = $ _ server ['http _ client_ip '];
  12. } Elseif (! Emptyempty ($ _ server ['http _ x_forwarded_for ']) {
  13. $ Ip = $ _ server ['http _ x_forwarded_for '];
  14. }
  15. Return $ ip;
  16. }
  17. /*

If the proxy is encrypted, the real IP address cannot be obtained.

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.