Today is Sunday
September 5, 2010

Search Results Tag: ip

June 22, 2010

PHP get IP Address

by Chris — Categories: Coding, PHP — Tags: , , , , No Comments

I have seen this done a hundred and one ways (figuratively speaking of course). However each way I have seen it there was always the occasion something would come up short, and the IP variable would come up undefined or empty. So it eventually lead me to having to tailor my own solution to obtain either my IP or a users IP depending on what my cause is.

Example: Your IP is: 38.107.191.105

How was this obtained? Through a rather simple process of elimination. Since there are multiple ways to get an IP address, yet not a single one of them works every time dependent on the user side of things. So what I have done is made a little function to try diffrent methods and then return the result to me. See the code below. It’s that simple. Although I am sure some of you are likely able to expand on this in a multitude of ways i’d be happy to see what come up with.

<?php
function getRealIpAddr(){

if(!empty($_SERVER['HTTP_CLIENT_IP'])){$ip=$_SERVER['HTTP_CLIENT_IP'];}

elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];}

else{$ip=$_SERVER['REMOTE_ADDR'];}

return $ip;
}
echo "Example: Your IP is: ".getRealIpAddr();
?>

© 2010 Monkey Tooth Productions All rights reserved - Wallow theme by TwoBeers Crew - Powered by WordPress - Have fun!