PHP Get User IP code implementation

Source: Internet
Author: User
PHP Get User IP code implementation

Getting a client User IP is often required in the Web site, such as:

1. Login IP: You can know in time whether your account is stolen;

2. Voting: can only do one vote per IP to prevent the swipe ticket;

3. Statistics: Statistical user sources, this is an indispensable part of the data analysis;

4. Other.

So it is important to get the client IP correctly, and here is the PHP code that gets the IP in daily use:

/**
* Get customer IP Address
* @return $ip
*/
function GetIP () {
if (!empty ($_server["Http_client_ip")) {
$ip = $_server["Http_client_ip"];
}
ElseIf (!empty ($_server["Http_x_forwarded_for")) {
$ip = $_server["Http_x_forwarded_for"];
}
ElseIf (!empty ($_server["REMOTE_ADDR")) {
$ip = $_server["REMOTE_ADDR"];
}
else{
$ip = "127.0.0.1";
}
return $IP;
}

This is just to provide reference, if you have any good way to communicate with the message!

  • 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.