/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Better IP Aquisition?

I wrote this to check for HTTP Forwarded (Proxy) but I know there
are better ways to get an IP. This one doesn’t seem very thorough,
and I’ll be GeoLocating IP’s based of it so would like to get the best
example I can find. Thanks Ahead guys.

[code=php]
if (getenv(HTTP_X_FORWARDED_FOR)){
$ip = @getenv(“HTTP_X_FORWARDED_FOR”);
explode(“,”,$ip);
trim($ip[sizeof($ip)-1]);
}

else {$ip = getenv(REMOTE_ADDR);}
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperApr 13.2010 — I've seen folks use this variation which approaches it logically:

[code=php]function getIP() {
$ip;
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "UNKNOWN";
return $ip;

} [/code]


Note: Use $_SERVER['blah'] instead of getenv("blah") if you run an ASAPI server, i.e. IIS. Above function is common for *nix. The advantage of getenv() over using $_SERVER outright is simply that it returns false on error, useful in functions like the one above, FYI. By no means the only way, isset($_SERVER["blah"]) works too!

-jim
Copy linkTweet thisAlerts:
@ehimeauthorApr 13.2010 — Thanks Jim, that should work great. I'm running Linux, so should be no conflicts
×

Success!

Help @ehime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...