Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

? asked in Computers & InternetSecurity · 7 years ago

How can I detect IP address through HTML code?

I am having some problem. I want to detect IP of someone who visiting my website. I know software tool by which we can detect IP but I wanna do this through HTML code or other programming language. How can I do this. Please suggest me some answer.

2 Answers

Relevance
  • ?
    Lv 5
    7 years ago
    Favorite Answer

    HTML cannot obtain IP addresses, PHP is required. Php is another programming language widely used for web development.

    Code to grab IP address:

    <?php

    echo "Your IP is";

    echo $_SERVER["REMOTE_ADDR"];

    function get_ip_address() {

    // check for shared internet/ISP IP

    if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))

    return $_SERVER['HTTP_CLIENT_IP'];

    // check for IPs passing through proxies

    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

    // check if multiple ips exist in var

    $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

    foreach ($iplist as $ip) {

    if ($this->validate_ip($ip))

    return $ip;

    }

    }

    if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))

    return $_SERVER['HTTP_X_FORWARDED'];

    if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))

    return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];

    if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))

    return $_SERVER['HTTP_FORWARDED_FOR'];

    if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))

    return $_SERVER['HTTP_FORWARDED'];

    // return unreliable ip since all else failed

    return $_SERVER['REMOTE_ADDR'];

    }

    function validate_ip($ip) {

    if (filter_var($ip, FILTER_VALIDATE_IP,

    FILTER_FLAG_IPV4 |

    FILTER_FLAG_IPV6 |

    FILTER_FLAG_NO_PRIV_RANGE |

    FILTER_FLAG_NO_RES_RANGE) === false)

    return false;

    self::$ip = $ip;

    return true;

    }

    ?>

  • 7 years ago

    use PHP

    <?PHP

    $ipaddress = $_SERVER["REMOTE_ADDR"];

    Echo "Your IP is $ipaddress!";

    ?>

Still have questions? Get your answers by asking now.