$_SERVER["REMOTE_ADDR"] gives server IP rather than visitor IP

Php

Php Problem Overview


I'm trying to track IP addresses of visitors. When using $_SERVER["REMOTE_ADDR"], I get the server's IP address rather than the visitor's. I tried this on multiple machines at multiple locations and they all resulted in the exact same IP. Is there some PHP/server settings that could be affecting this?

Php Solutions


Solution 1 - Php

> When using $_SERVER["REMOTE_ADDR"], I get the server's IP address rather than the visitor's.

Then something is wrong, or odd, with your configuration.

  • Are you using some sort of reverse proxy? In that case, @simshaun's suggestion may work.

  • Do you have anything else out of the ordinary in your web server config?

  • Can you show the PHP code you are using?

  • Can you show what the address looks like. Is it a local one, or a Internet address?

Solution 2 - Php

$_SERVER['REMOTE_ADDR'] gives the IP address from which the request was sent to the web server. This is typically the visitor's address, but in your case, it sounds like there is some kind of proxy sitting right before the web server that intercepts the requests, hence to the web server it appears as though the requests are originating from there.

Solution 3 - Php

Look no more for IP addresses not being set in the expected header. Just do the following to inspect the whole server variables and figure out which one is suitable for your case:

print_r($_SERVER);

Solution 4 - Php

Replacing:

$_SERVER["REMOTE_ADDR"];

With:

$_SERVER["HTTP_X_REAL_IP"];

Worked for me.

Solution 5 - Php

Try this

$_SERVER['HTTP_CF_CONNECTING_IP'];

instead of

$_SERVER["REMOTE_ADDR"];

Solution 6 - Php

If you are using Cloudflare then this is always the Cloudflare IP address from the node which is serving you.

In this case you get the real IP address from the $_SERVER['HTTP_FORWARDED_FOR'] entry as described in the the other answers.

Solution 7 - Php

if your site is behind cloudflare, you can use another header provided by cloudflare itself:

$_SERVER['HTTP_CF_CONNECTING_IP']

or if you are using Laravel

$request->server('HTTP_CF_CONNECTING_IP');

read more about it here:

How to get real client IP behind Cloudflare in Laravel / PHP

Solution 8 - Php

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

Solution 9 - Php

#php 7.x

<?php
$ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
echo "The user IP Address is - ". $ip;
?>

from https://www.delftstack.com/howto/php/php-get-user-ip/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLincecumView Question on Stackoverflow
Solution 1 - PhpPekkaView Answer on Stackoverflow
Solution 2 - PhpcasablancaView Answer on Stackoverflow
Solution 3 - PhphammadyView Answer on Stackoverflow
Solution 4 - PhpchiappaView Answer on Stackoverflow
Solution 5 - PhpSanuView Answer on Stackoverflow
Solution 6 - PhpPeter VARGAView Answer on Stackoverflow
Solution 7 - PhpIgor SimicView Answer on Stackoverflow
Solution 8 - PhpFadhel AliView Answer on Stackoverflow
Solution 9 - PhpPeterView Answer on Stackoverflow