Use IP or LOCATION the user is (or closest to) to send a query

Asked

Viewed 160 times

2

I have the following code:

function getpage($url, $postdata=''){
    $c = curl_init(); <br>
    curl_setopt($c, CURLOPT_URL, $url); 
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false); 
    curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false); 

    curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1)
    AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2'); 
    curl_setopt($c, CURLOPT_POST, 1); <br>
    curl_setopt($c, CURLOPT_POSTFIELDS, $postdata);

What happens is that I can even use a proxy, but that’s not really what I want. See how it would look with proxy:

   function getpage($url, $postdata=''){
   $proxy = 'UM-IP';
   $porta = 'A-PORTA';
   $c = curl_init();
   curl_setopt($c, CURLOPT_URL, $url);
   curl_setopt($c, CURLOPT_PROXY, $proxy);
   curl_setopt($c, CURLOPT_PROXYPORT, $porta);
   curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);
   curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);

   curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) 
   AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2'); 
   curl_setopt($c, CURLOPT_POST, 1);
   curl_setopt($c, CURLOPT_POSTFIELDS, $postdata);

What I wish to do is when the user enters the page of my site, and enter the data that will be requested. that the query be executed, and show that this was done perhaps by his IP or a point near it.

I’ve tried many ways, but without success.

I doubt why it doesn’t work when I put it in the proxy: $_SERVER['REMOTE_ADDR'] and at the door: $_SERVER['REMOTE_PORT']?

In my view this would be requesting his ip plus the port, only when I try to use, it does not work. How can I achieve this?

Thanks for the answers, but this function of discovering the IP can already easily with these codes:

function getpage($url, $postdata='')  
{  
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {  
    $ip = $_SERVER['HTTP_CLIENT_IP'];  
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
} else {  
    $ip = $_SERVER['REMOTE_ADDR'];  
}  
echo $ip;

What I need now, I don’t know if it’s something simple, but I’m looking for it in time is how do I deploy it correctly in the code I have:

$c = curl_init();  
curl_setopt($c, CURLOPT_URL, $url);  
curl_setopt($c, CURLOPT_PROXY, $proxy);  
curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);  
curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);  
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);  
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1)   AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2');  
curl_setopt($c, CURLOPT_POST, 1);  
curl_setopt($c, CURLOPT_POSTFIELDS, $postdata);  

Maybe that line:

curl_setopt($c, CURLOPT_PROXY, $proxy);  

But even if I change $proxy for $ip, still not sure. I have read all the links that were passed as well, and I did many more tests yet unsuccessfully.

  • I believe that that site you passed as an example goes against accepted rules in the community. I removed from your question.

2 answers

0

// Função para obter o IP PHP
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

//Server para obter IP do Cliente
function get_client_ip() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
    $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
    $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
    $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
    $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
    $ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
    $ipaddress = $_SERVER['REMOTE_ADDR'];
else
    $ipaddress = 'UNKNOWN';
return $ipaddress;
}

I had this function for a similar situation and with me it worked, I hope it also

  • -1 This has severe security vulnerabilities allowing IP Spoofing. You trust the header sent by the user, ie if send curl seusite.com -H "X-Forwarded-For: 123.123.123.123" you will get the fake IP. Off that the X_FORWARDED_FOR (do not know the rest) can have several Ips, IE 111.111.111.111, 222.222.222.222, Anyway he is not reliable.

0

It’s exactly the same as this question. What you want to do is not possible, at least not a mere site. Other than there are security breaches in the code, but I’ll ignore that.


For you to use some IP as proxy it must be a proxy, i.e., it must be a proxy server. In fact, many malware do this just so that that infected device serves as a proxy.

In general it is the same as I take your IP and want to do http://(seu-ip) and wait for a site to open, this will only work if your IP has any HTTP server connected, but it will give error of connection to your IP using HTTP.

If it were not so anyone could spoof an IP simply by using as proxy a third party IP.


How do they do?

Simple, hire proxy servers around the world and use the proxy closest to the person’s location or hire servers and install a Squid. Another option is to create an installable software on the client’s machine (a smartphone application and an installable program on the PC) that can execute such a request, logically it will leave using the client’s IP.

There are several and several companies who sell proxies around the world, for example:

There are also services known as "backconnect proxies", they are Ips of residential networks, usually originated from botnet, there is no way to know the origin of these Ips and these servers. They also change the IP every X minutes most of the time, it’s them:

I have no link with any site mentioned above!

The other solution, as I mentioned, is to have your own proxy server, for example using the Squid, so just hire dedicated servers around the world and install the software. Now, on Curl you will be able to connect to it and use it as a proxy, logically requires some settings.

  • Thank you for your answers, you’ve been a great help;

Browser other questions tagged

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