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?
$_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']
Yes, it's safe. It is the source IP of the TCP connection and can't be substituted by changing an HTTP header.
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.
$_SERVER['REMOTE_ADDR'] gives the IP address from which the request was sent to the web server.
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?
$_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With