简体   繁体   中英

How to find IP address of a client (customer)

I need the client(customer) ip address of the browser.

This is the client page in php

<?php
    $xml_request = '<XMLRequest>
    <RequestType>ServiceRequest</RequestType>
    <RequestLogin>test</RequestLogin>
    </XMLRequest>';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://testserver.com/test_xml/request.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_ENCODING,'gzip');
    curl_setopt($ch, CURLOPT_POST, true ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_request);
    echo curl_exec($ch); curl_close($ch);
?>

This is my server page 'http://testserver.com/test_xml/request.php' in php

$service_request = file_get_contents('php://input');

echo  'Client IP Address: '. $_SERVER['REMOTE_ADDR'];

But here its 'ECHO' my own server IP.

How i get the client browser IP to my server

Your're doing http request from your own server, that's why you get your server IP. To solve your problem you have to make changes in client page (eg. add additional parameter to XML Request or add custom header with client IP address)

Thank you.

Are you hosting the website? Then it would show your own IP, when you viewed the website.

It will not work like that because you call the Server page from your client page, which both sit on your server and use PHP. So in your scenario the server is asking itself for his IP address (client & server are the same).

You could put $_SERVER['REMOTE_ADDR'] on your client page. But I also don't understand why you have a 'client' and a 'server' page both written in PHP and one calls the other with curl. It seems like horrible practice.

As answered by Jumes, you are using the correct Server variables. If you run your script from your server console [or by some other method] , your server Ip address will show up.

You may want to try running the client script from different machines [having different public IP addresses] to see a visible difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM