Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: getting ip address

I want to get an ip address of visitors. could you tell me what element of $_SERVER[] i should use?

$_SERVER['HTTP_CLIENT_IP'];
$_SERVER['HTTP_X_FORWARDED_FOR'];
or
$_SERVER['REMOTE_ADDR'];

UPDATE:

If your client is connected to the Internet through Proxy Server then $_SERVER['REMOTE_ADDR'] in PHP just returns the the IP address of the proxy server not of the client’s machine.There are extra Server variable which might be available to determine the exact IP address of the client’s machine in PHP, they are HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR.

like image 727
Simon Avatar asked Apr 18 '26 10:04

Simon


2 Answers

$_SERVER['REMOTE_ADDR'];

According to the PHP documentation: The IP address from which the user is viewing the current page.

This is the IP that is connected to your server (reported by your server).
The other values are set by the client.

The HTTP_X_FORWARDED_FOR is a non-standard header (hence the x-prefix), set by certain proxy-servers. It is an attempt by the big proxy server vendors to help ISPs identify abusive IP addresses; it contains a list with all forwarded-for IPs.

I don't know the origin of the HTTP_CLIENT_IP header

like image 106
Jacco Avatar answered Apr 21 '26 00:04

Jacco


It depends on if your application is behind a reverse proxy or something like that. The easiest thing to do is check all three and choose the one that is not one of your IPs.

like image 30
Joshua Smith Avatar answered Apr 21 '26 00:04

Joshua Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!