0

I use this simple script:

<script type="text/javascript">
var _mytrack = _mytrack || [];
_mytrack.push(['_setType', 'SOMETYPE']);
_mytrack.push(['_setEmail', '[email protected]']);

(function() {
var mytrack = document.createElement('script');
_mytrack.type = 'text/javascript';
_mytrack.async = true;
mytrack.src = ('https:' == document.location.protocol ? 'https://www' : 'http://www') + '.mytrack.net/tracker.tell.php?i='+_mytrack;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mytrack, s);
})();
</script>

My PHP script (tracker.tell.php) looks like this:

header("content-type: application/javascript");
echo "alert('".$_SERVER['REMOTE_ADDR']."');";

Now the question is related to the $_SERVER['REMOTE_ADDR'] because this gives (in an JS alert) the IP of the SERVER and not of the CLIENT.

8
  • Might not be possible to do with PHP because the PHP script runs on the server, not the client. Have you tried/considered using Javascript for this? Commented Oct 22, 2013 at 13:18
  • 1
    @jonhopkins Of course you can get the remote IP from PHP... how do you think the web server communicates with the client? Commented Oct 22, 2013 at 13:22
  • You're probably running a proxy server on your server, yeah? Commented Oct 22, 2013 at 13:23
  • Hi @Brad yes I'm using a proxy BUT when I go to simple: "show my IP websites" they give the correct one... Commented Oct 22, 2013 at 13:26
  • 1
    @bvl The remote address is always the address which has made a TCP connection to the server. If you're using a proxy, that's the address of the proxy. Most proxy servers pass on a header with the address of the end client. X-Forwarded-For is most common. Commented Oct 22, 2013 at 14:07

1 Answer 1

3

$_SERVER['REMOTE_ADDR'] does give you the IP address of the remote user. But if you are using a proxy or load balancer, you should use $_SERVER['HTTP_X_FORWARDED_FOR']

If users can reach your server directly or through a proxy then just use an if statement

$remote_ip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);

then pass the variable to JS.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.