Indentifying real IP address in PHP

Network Add comments

Did you know that using $_SERVER['REMOTE_ADDR'] to get visitor’s IP address sometimes is not accurate? If your site’s visitor is connected to the Internet via Proxy Server then $_SERVER['REMOTE_ADDR'] in PHP just returns the the IP address of the proxy server and not the IP address of the client’s machine. $_SERVER variable might have a few more useful things available to determine the exact IP address of the client’s machine in PHP, they are HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR.

Here is a really simple function in PHP to find the real IP address of the client’s machine.

  1. <?php
  2. function ThisIP() {
  3.  
  4.     if( array_key_exists(‘HTTP_CLIENT_IP’, $_SERVER) ) {
  5.         return $_SERVER[‘HTTP_CLIENT_IP’];
  6.     }
  7.  
  8.     if( array_key_exists(‘HTTP_X_FORWARDED_FOR’, $_SERVER) ) {
  9.         $ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];
  10.         if(stristr($ip,",") != "") {
  11.             $ips = explode(",",$ip);
  12.  
  13.             if($ips[count($ips)-1] != ""){
  14.                 $ip = trim($ips[count($ips)-1]);
  15.             } else {
  16.                 $ip = trim($ips[0]);
  17.             }
  18.  
  19.             switch (substr($ip,0,3)) {
  20.                 case "192":
  21.                 case "10.":
  22.                 case "127":
  23.                     if( array_key_exists(‘REMOTE_ADDR’, $_SERVER) ) {
  24.                         return $_SERVER[‘REMOTE_ADDR’];
  25.                     }
  26.                 break;
  27.             }
  28.         }
  29.         return $ip;
  30.     }
  31.  
  32.     if( array_key_exists(‘REMOTE_ADDR’, $_SERVER) ) {
  33.         return $_SERVER[‘REMOTE_ADDR’];
  34.     }
  35.  
  36.     return false;
  37. }
  38. ?>

32 Responses to “Indentifying real IP address in PHP”

  1. mirv7 Says:

    You need to be careful to think about why you’re needing the IP address. The client’s “real” IP address is a self-reported thing when using this technique. Any client could spoof it. The host IP address may be that of a proxy, but it isn’t spoofed. So they really are different pieces of information, to be used for different purposes. The “real” IP addr of many clients these days is likely to be 192.168.1.101 which is not especially valuable in many cases.

  2. Jeff Says:

    Great article, thanks!

  3. ARTURO Says:

    Abilify@official.site” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/my.gardenguides.com’);”>.

    Buywithout prescription…

  4. DERRICK Says:

    Cialis discount

    Buyno prescription…

  5. ERNEST Says:

    purchase@effexor.now” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/my.gardenguides.com’);”>.

    Buyno prescription…

  6. JESSIE Says:

    Zyvox

    Buydrugs without prescription…

  7. SEAN Says:

    Zyrtec

    Buydrugs without prescription…

  8. DON Says:

    xeloda

    Buynow…

  9. PHILIP Says:

    xeloda bleeding

    Buygeneric meds…

  10. REX Says:

    Zoloft

    Buygeneric drugs…

  11. DENNIS Says:

    Voltaren

    Buyno prescription…

  12. HERMAN Says:

    Xenical

    Buynow…

  13. WESLEY Says:

    Vitamin E

    Buynow…

  14. DUSTIN Says:

    pyrantel pamoate gets rid of which worms

    Buydrugs without prescription…

  15. STUART Says:

    pediatric@griseofulvin.liver.tumors” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>…

    Buygeneric pills…

  16. IVAN Says:

    do@you.need.to.fast.for.prograf.levels” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>…

    Buywithout prescription…

  17. GLEN Says:

    .

    Buydrugs without prescription…

  18. ANTONIO Says:

    purpose@of.advair” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>.

    Buygeneric meds…

  19. WILLARD Says:

    if@you.are.allergic.to.ceclor.can.you.take.augmentin” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>…

    Buywithout prescription…

  20. JULIAN Says:

    bulk@coral.calcium.powder” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>..

    Buygeneric drugs…

  21. STEVE Says:

    accupril@side.affects” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>…

    Buyno prescription…

  22. EDDIE Says:

    does@clonidine.and.lamisil.interaction” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>..

    Buywithout prescription…

  23. BERNARD Says:

    sexual@side.effects.of.lexapro.vs.paxil” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>..

    Buynow it…

  24. CLAUDE Says:

    aciphex@vs.nexium” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>..

    Buygeneric drugs…

  25. WARREN Says:

    pulmicort@or.xopenex.first.when.using.both” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>…

    Buyit now…

  26. ALFRED Says:

    where@to.buy.acai.berry.zen” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>…

    Buyno prescription…

  27. CARLTON Says:

    flomax@in.dogs” rel=”nofollow” onclick=”javascript:pageTracker._trackPageview (’/outbound/www.box.net’);”>.

    Buyit now…

  28. TED Says:

    ..

    Buynow it…

  29. jhelles Says:

    Hello…

    My life,vist it http://juhuacha.blogonsisters.com/buzz/letter-wedding-cake-topper/ ,Thanks….

  30. ghesrker Says:

    Hello…

    My life,vist it http://www.travelblog.in/next/next.php?membername=juhuacha ,Thanks….

  31. Frederics Says:

    Hello…

    My life,vist it http://www.travelblog.in/next/next.php?membername=juhuacha ,Thanks….

  32. Chantelles Says:

    Hello…

    My life,vist it http://xiangdress.zoomblog.com/ ,Thanks….

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in