About Linux FAQ

Browse More FAQs:

How to determine or retrieve visitor’s IP address using PHP

Posted by Vivek Gite [Last updated: May 11, 2008]

Q. How do I determine or retrieve visitor's IP address using PHP server side programming?

A. Use following enviorment variable to get visitor's IP address:

No Proxy detection
=> REMOTE_ADDR - Remote client IP address

With Proxy detection
=> HTTP_X_FORWARDED_FOR - Get Proxy server IP

PHP getenv() function
=> Use php getenv() function to read the value of the environment.

Here is PHP code (download source code link):

<html>
<head>
 <title>What is my IP address?</title>
</head>
<body>
<?php
 
    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $pipaddress = getenv(HTTP_X_FORWARDED_FOR);
        $ipaddress = getenv(REMOTE_ADDR);
echo "Your Proxy IPaddress is : ".$pipaddress. "(via $ipaddress)" ;
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
        echo "Your IP address is : $ipaddress";
    }
?>
</body>
</html>
 

Try the demo here

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Abhishek Says:

    it is working on localhost
    but it doesn’t work after uploading it on website
    i m getting blank page..

  2. Marvin Henry Says:

    I tried with the given php code “to determine or retrieve visitors ip address in PHP” in two different machines and got the same ip address.

  3. Rahman Says:

    I want to get visitor IP address into table of my database.

    please help me

    thanks

  4. trupti Says:

    i am getting a blank page after i paste the code onto my website. plz help.

  5. David Says:

    I never got it to work. The host always displays an error about an unexpected & when expecting ;
    But here is a better way, just remember to use the as needed:
    http://www.plus2net.com/php_tutorial/php_ip.php

  6. Ogaden Says:

    Excellent tutorial!

    It works for me. I havent uploaded it to my site yet. But it looks find and i guess it should work.

    Thanks :)

  7. erti Says:

    In line 12 and 15 delete and rewrite the ” and it will work.

  8. RK Aggarwal Says:

    very nicely given. thanx a tone

  9. Muhammad Says:

    The tutorial is really great it solved my problem at once…On my local machine and on my webserver it worked.

    Thanx,
    Umair

  10. Deepak Poolamadai Says:

    Hi All
    This code worked for me fine……
    Thanks to the programmer……

    And For this
    # Marvin Henry Says:
    February 6th, 2007 at 7:42 am

    I tried with the given php code “to determine or retrieve visitors ip address in PHP” in two different machines and got the same ip address.

    Reply
    I think u get a Ip address from the DHCP server.
    In This case You should the ip address of the Server and not of the client Machine.

    Anyways this solved my problem….
    Thanks a ton.

    Deepak Poolamadai

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.