PHP Script to Show Visitor’s IP Address

Dzhuneyt Ahmed - Author of this post

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 like X-Forwarded-For instead.

PHP Script to Show Visitor’s IP Address

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.

Dzhuneyt Ahmed

Dzhuneyt

Helping teams build reliable cloud infrastructure — without the bloated bill.

Social

My Other Blogs

© 2026 Dzhuneyt Ahmed. All rights reserved.