my public IP tool help?

Author
Chisom Balogun Author
|
1 day ago Asked
|
15 Views
|
2 Replies
0

Hello everyone, first time posting here! i just launched my very first web tool, a simple "What is my IP Address" site. it's super basic, just trying to learn the ropes of web dev and saas. but i've hit a wall and i'm hoping some experienced folks here can point me in the right direction.

The main issue is that my tool isn't consistently showing the correct public ip address for users. sometimes it shows an internal network ip (like 192.168.x.x) or an ip that just doesn't match what other "what is my ip" sites show. it's really frustrating because the whole point of the tool is to show *their* public ip.

i've tried a few things already. initially, i was just using $_SERVER['REMOTE_ADDR'] in php. when that didn't work right, i found some code snippets online that suggested checking for proxy headers, like HTTP_CLIENT_IP or HTTP_X_FORWARDED_FOR. i even tried to build a small function to loop through them:

function get_user_ip() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; // Might contain multiple IPs
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
// and then i call it like: echo "Your IP is: " . get_user_ip();

but even with that, i'm still seeing weird ips. for example, sometimes i get something like 172.x.x.x which i know isn't a public ip, or sometimes it's just plain wrong compared to what my phone's browser shows on another site. i'm hosting on a shared server, if that makes any difference.

i'm trying to figure out the most reliable way to get a user's actual public ip address. is there a standard method i'm missing? or maybe something about server configuration i need to check?

2 Answers

0
Karan Mehta
Answered 1 day ago
The main issue is that my tool isn't consistently showing the correct public IP address for users.
Your server, especially on shared hosting or behind a CDN, often sees the IP of a proxy or load balancer, not the user's true public IP, leading to internal network addresses or incorrect values. For reliable public IP detection, it's best to perform a client-side lookup via JavaScript using a dedicated external IP API service, as server-side $_SERVER variables are prone to misrepresentation or spoofing. Consider using services like ipify.org or ipinfo.io; our What is my IP Address tool also relies on robust methods for accurate public IP lookup. Hope this helps your conversions!
0
Chisom Balogun
Answered 17 hours ago

Wow, appreciate this Karan. Seriously impressed by your depth of knowledge on this stuff, gives me a lot to think about!

Your Answer

You must Log In to post an answer and earn reputation.