0

I have an id.php page on one Domain that creates a unique visitor "id" that uses the user's IP address: that is displayed like this "Client ID : aeiufweiu2h5" the user's IP address is 182.55.68.2

I'm trying to do a file_get_contents() from that page to another Domain "idtest.php" to put the content of the page in a string, but the problem is this page makes the request with the server IP address.

The result is always the same "Client ID : 2ghg43jjjh5443" the IP address of the server is 192.55.0.2 (this is the IP of the php server)

I tried curl, regex and I have a dynamic IP address but the result is the same.

Is there any way to do this whit the clients user IP-address?

Javascript, ajax, jsonp, jquery.....?

JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • Why is java tag used? – anirudh Apr 10 '14 at 13:33
  • Does the client connect to both? Or are you getting the file for the client from server/domain 2? If the later, you may be able to use [X-Forwarded-For](http://en.wikipedia.org/wiki/X-Forwarded-For) - which is for proxies. – Elliott Frisch Apr 10 '14 at 13:35
  • The simple answer is NO. But you should describe the problem you are trying to solve this way. Which parties are considered *trusted* (out of the three)? – vbence Apr 10 '14 at 13:40

3 Answers3

0

If you perform the request from the server, using a server-side language, it will always come from the server IP address.

If you can return some html and JS and use the client's browser, you can make it work using AJAX to send the request, that way it is the client who is actually sending that request.

For example, in the domain 1, you can test this with a simple jQuery AJAX request:

$( "body" ).load( "http://otherdomain.com/page.html" );

This way on otherdomain.com/page.html you will receive a request coming from that user's IP address.

As @JanDvorak pointed in the comments, the second domain will need to set the Access-Control-Allow-Origin header in the second domain (Although there are other methods)

Access-Control-Allow-Origin: http://www.firstdomain.com

If the request has to be done using the server, you could use the headers to send the client's IP address, using the header X-Forwarded-for (which is normally used by proxies), although you could use any custom header if you prefer. That way you will be able to access the client IP in the second domain.

If you are using PHP on the second server (I supposed so by your tags) take a look at this question in order to get the IP from the custom header.

Community
  • 1
  • 1
aurbano
  • 3,324
  • 1
  • 25
  • 39
0

Code ran in PHP or other serverside languages will show your server. Simply run with client side languages such as JS or JQuery.

You can get the contents of the remote file using a JQuery $.GET function ie

$.get( "myOtherDomain.com/idtest.php", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

To combat cross-site scripting (XSS) on your php file, set cross-domain requests on. You can either do all websites (*) or your own domains (mydomain.com).

//PHP
header('Access-Control-Allow-Origin: *');
Dean Meehan
  • 2,511
  • 22
  • 36
0

If you call from one server-side(php file) to another server-side(php file), It will always give you server ip address not client ip address. If you want to get Client IP address you have to make call from ajax(JS or Jquery) rather than one server to other... and send result to other server using ajax(JS or Jquery) call.

John Dvorak
  • 26,799
  • 13
  • 69
  • 83