Get Client IP Address in PHP
The most reliable way to get client IP in PHP is with $_SERVER['REMOTE_ADDR']:
$ipaddress = $_SERVER['REMOTE_ADDR']; echo "Your IP address is: ".htmlspecialchars($ipaddress);
Your IP address is: 220.127.116.11
REMOTE_ADDR is an environment variable provided by the web server and cannot be modified.
You should not use
$_SERVER['HTTP_X_FORWARDED_FOR'] because it can be modified\spoofed!
You can get the IP hostname with gethostbyaddr(), example:
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); echo "Your hostname is: ".htmlspecialchars($hostname);
Your hostname is: ec2-54-158-15-97.compute-1.amazonaws.com