Script to query external IP

Asked

Viewed 3,015 times

6

How can I get the address of IP of the person who logged on to my system?

I Googled and all I find are queries on third-party websites (https://api.ipify.org/, http://ident.me/, among others) who bring the IP.

Does anyone know a link or script (PHP, Python,...) that does this?

3 answers

2

On the link you posted https://api.ipify.org/ adding ?format=json he returns the Ipin format Json. The full link you can view here.

You can get the Ipusing the function getJSONjquery.

Following example.

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $.getJSON("https://api.ipify.org?format=json", function (data) {
                alert(data.ip);
            });
        });
       
    </script>
</body>
</html>

1

I solved it this way

function getIP() {
    if($_SERVER["HTTP_X_FORWARDED_FOR"]) {
        $proxy = '';
        if($_SERVER["HTTP_CLIENT_IP"]) {
            $proxy = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $proxy = $_SERVER["REMOTE_ADDR"];
        }

        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    } else {
        if($_SERVER["HTTP_CLIENT_IP"]) {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $ip = $_SERVER["REMOTE_ADDR"];
        }
    }

    if (empty($proxy)) {
        return $ip;
    } else {
        return $proxy;
    }
}

0

These links are returning the ip your machine is using (client)

to get the same ip through php you can use this code:

<?php echo $_SERVER['REMOTE_ADDR']; ?>

If you want a function that returns an IP with a higher degree of certainty, you have this function:

<?php

if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 
    echo $_SERVER['HTTP_CLIENT_IP'];
} else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
    echo $_SERVER['HTTP_X_FORWARDED_FOR']; }
else { echo $_SERVER['REMOTE_ADDR']; }

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.