Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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?

like image 461
Lincecum Avatar asked Nov 23 '10 23:11

Lincecum


People also ask

What is $_ server [' Remote_addr ']?

$_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the current page. $_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the current page. $_SERVER['REMOTE_PORT']

Can $_ server Remote_addr be spoofed?

Yes, it's safe. It is the source IP of the TCP connection and can't be substituted by changing an HTTP header.

How can I get public IP address in PHP?

The simplest way to collect the visitor IP address in PHP is the REMOTE_ADDR. Pass the 'REMOTE_ADDR' in PHP $_SERVER variable. It will return the IP address of the visitor who is currently viewing the webpage.

What is _server Remote_addr?

$_SERVER['REMOTE_ADDR'] gives the IP address from which the request was sent to the web server.


2 Answers

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?

like image 161
Pekka Avatar answered Sep 21 '22 12:09

Pekka


$_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.

like image 45
casablanca Avatar answered Sep 20 '22 12:09

casablanca