PHP Script to Show Visitor’s IP Address
By: Dzhuneyt Ahmed
Posted · 1 min read
Outdated: This post references
REGISTER_GLOBALS, which was deprecated in PHP 5.3 and removed in PHP 5.4. Use$_SERVER['REMOTE_ADDR']exclusively. Note that behind a reverse proxy or load balancer, you may need to check headers likeX-Forwarded-Forinstead.
Sometimes you want to get the visitor’s IP address (e.g to show it to him or for logging purposes). This simple snippet does the job:
$ip = $_SERVER['REMOTE_ADDR'];
Also, if you have REGISTER_GLOBALS turned on on your server, you can use the shorter version:
$ip = @$REMOTE_ADDR;
Then you can easily use the variable called $ip for your needs.

