how to get client's IP address in PHP
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: how to get client's IP address in PHP

  1. #1
    Junior Member
    Join Date
    Aug 2006
    Posts
    7

    how to get client's IP address in PHP

    I have installed PHPTRAID to check my php codes. The following code
    <?php echo $_SERVER['REMOTE_ADDR'];?> will give 127.0.0.1 as output. but i want the exact ip address (192.*.*.*) of the client.

    The following java code ( in JSP page)
    InetAddress i;
    i=InetAddress.getLocalHost();
    running on the same m/c will give the output as 192.*.*.*;
    only the php code is not responding. my m/c is in LAN. when i view the
    "Local Area Connection Status", my m/c has its ip address as 192.*.*.*;

    Moreover, <?php
    header("Location: http://www.google.com/");
    ?>
    will give the warning as
    Warning: Cannot add header information - headers already sent by (output started at c:\apache\htdocs\redirect.php:3) in c:\apache\htdocs\redirect.php on line 5.

    help me.

  2. #2
    Junior Member
    Join Date
    Aug 2006
    Posts
    28
    Try $_SERVER['REMOTE_HOST'] and have a look at the discussions
    here and here.

  3. #3
    Junior Member
    Join Date
    Aug 2006
    Posts
    7
    thanks for reply.
    $_SERVER['REMOTE_HOST'] doesn't give any output.

  4. #4
    Senior Member
    Join Date
    Mar 2003
    Posts
    209
    PHP Code:
    $_SERVER['REMOTE_ADDR']; 
    check out all the predefined $_SERVER variables here

  5. #5
    Junior Member
    Join Date
    Aug 2006
    Posts
    7
    $SERVER[REMOTE_HOST] doesn't give any output.

    $SERVER[REMOTE_ADDR] gives local host's address(127.0.0.1 )as output. but i need client's ip address(192.*.*.*).

  6. #6
    Senior Member
    Join Date
    Mar 2003
    Posts
    209
    when i use $_SERVER['REMOTE_ADDR']; i get my IP address..

    however, the ip address that you may have been told of by your ISP and the one that requests page information may be different if you use proxy or routers, etc..

  7. #7
    Junior Member
    Join Date
    Aug 2006
    Posts
    7
    i have checked all server variables.
    <center><b>Server variables</b></center>
    <hr>
    GATEWAY_INTERFACE = <?php echo $_SERVER['GATEWAY_INTERFACE']; ?> <br>
    SERVER_ADDR = <?php echo $_SERVER['SERVER_ADDR']; ?> <br>
    SERVER_NAME = <?php echo $_SERVER['SERVER_NAME']; ?> <br>
    SERVER_SOFTWARE = <?php echo $_SERVER['SERVER_SOFTWARE']; ?> <br>
    SERVER_PROTOCOL = <?php echo $_SERVER['SERVER_PROTOCOL']; ?> <br>
    REQUEST_METHOD = <?php echo $_SERVER['REQUEST_METHOD']; ?> <br>
    REQUEST_TIME = <?php echo $_SERVER['REQUEST_TIME']; ?> <br>
    QUERY_STRING = <?php echo $_SERVER['QUERY_STRING']; ?> <br>
    DOCUMENT_ROOT = <?php echo $_SERVER['DOCUMENT_ROOT']; ?> <br>
    HTTP_ACCEPT = <?php echo $_SERVER['HTTP_ACCEPT']; ?> <br>
    HTTP_ACCEPT_CHARSET = <?php echo $_SERVER['HTTP_ACCEPT_CHARSET']; ?> <br>
    HTTP_ACCEPT_ENCODING = <?php echo $_SERVER['HTTP_ACCEPT_ENCODING']; ?> <br>
    HTTP_ACCEPT_LANGUAGE = <?php echo $_SERVER['HTTP_ACCEPT_LANGUAGE']; ?> <br>
    HTTP_CONNECTION = <?php echo $_SERVER['HTTP_CONNECTION']; ?> <br>
    HTTP_HOST = <?php echo $_SERVER['HTTP_HOST']; ?> <br>
    HTTP_REFERER = <?php echo $_SERVER['HTTP_REFERER']; ?> <br>
    HTTP_USER_AGENT = <?php echo $_SERVER['HTTP_USER_AGENT']; ?> <br>
    HTTPS = <?php echo $_SERVER['HTTPS']; ?> <br>
    REMOTE_ADDR = <?php echo $_SERVER['REMOTE_ADDR']; ?> <br>
    REMOTE_HOST = <?php echo $_SERVER['REMOTE_HOST']; ?> <br>
    REMOTE_PORT = <?php echo $_SERVER['REMOTE_PORT']; ?> <br>
    SCRIPT_FILENAME = <?php echo $_SERVER['SCRIPT_FILENAME']; ?> <br>
    SERVER_ADMIN = <?php echo $_SERVER['SERVER_ADMIN']; ?> <br>
    SERVER_PORT = <?php echo $_SERVER['SERVER_PORT']; ?> <br>
    SERVER_SIGNATURE = <?php echo $_SERVER['SERVER_SIGNATURE']; ?> <br>
    PATH_TRANSLATED = <?php echo $_SERVER['PATH_TRANSLATED']; ?> <br>
    SCRIPT_NAME = <?php echo $_SERVER['SCRIPT_NAME']; ?> <br>
    REQUEST_URI = <?php echo $_SERVER['REQUEST_URI']; ?> <br>
    PHP_AUTH_DIGEST = <?php echo $_SERVER['PHP_AUTH_DIGEST']; ?> <br>
    PHP_AUTH_USER = <?php echo $_SERVER['PHP_AUTH_USER']; ?> <br>
    PHP_AUTH_PW = <?php echo $_SERVER['PHP_AUTH_PW']; ?> <br>
    AUTH_TYPE = <?php echo $_SERVER['AUTH_TYPE']; ?> <br>

    by executing the above PHP codes, i got output as

    Server variables

    --------------------------------------------------------------------------------
    GATEWAY_INTERFACE = CGI/1.1
    SERVER_ADDR = 127.0.0.1
    SERVER_NAME = localhost
    SERVER_SOFTWARE = Apache/1.3.23 (Win32)
    SERVER_PROTOCOL = HTTP/1.1
    REQUEST_METHOD = GET
    REQUEST_TIME =
    QUERY_STRING =
    DOCUMENT_ROOT = c:/apache/htdocs
    HTTP_ACCEPT = */*
    HTTP_ACCEPT_CHARSET =
    HTTP_ACCEPT_ENCODING = gzip, deflate
    HTTP_ACCEPT_LANGUAGE = en-us
    HTTP_CONNECTION = Keep-Alive
    HTTP_HOST = localhost
    HTTP_REFERER =
    HTTP_USER_AGENT = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    HTTPS =
    REMOTE_ADDR = 127.0.0.1
    REMOTE_HOST =
    REMOTE_PORT = 1642
    SCRIPT_FILENAME = c:/apache/php/php.exe
    SERVER_ADMIN = admin@localhost
    SERVER_PORT = 80
    SERVER_SIGNATURE =
    Apache/1.3.23 Server at localhost Port 80

    PATH_TRANSLATED = c:\\apache\\htdocs\\test4.php
    SCRIPT_NAME = /php/php.exe
    REQUEST_URI = /test4.php
    PHP_AUTH_DIGEST =
    PHP_AUTH_USER =
    PHP_AUTH_PW =
    AUTH_TYPE =

    please help me to figure out what is going wrong with my codes.

  8. #8
    Junior Member 8ta8ta's Avatar
    Join Date
    Aug 2006
    Posts
    22
    PHP Code:
    <?php
    echo ' Client IP: ';
    if ( isset(
    $_SERVER["REMOTE_ADDR"]) )    {
        echo 
    '' $_SERVER["REMOTE_ADDR"] . ' ';
    } else if ( isset(
    $_SERVER["HTTP_X_FORWARDED_FOR"]) )    {
        echo 
    '' $_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
    } else if ( isset(
    $_SERVER["HTTP_CLIENT_IP"]) )    {
        echo 
    '' $_SERVER["HTTP_CLIENT_IP"] . ' ';
    }
    ?>

  9. #9
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    The reason you're getting 127.0.0.1 from REMOTE_ADDR is because that *is* your remote address. If you type in http://localhost/ into your browser, you're using the loopback interface 127.0.0.1. This interface is entirely separate from your actual LAN address of 192.x.x.x that you're expecting.

    If you type in http://192.12.34.56/ (using your real internal IP instead), you'll see 192.12.34.56 returned. Why? Because you're connecting to Apache on a totally different interface.

  10. #10
    Junior Member
    Join Date
    Aug 2007
    Posts
    1
    I use this function :

    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;

    }

    refer : http://admincmd.blogspot.com

  11. #11
    Junior Member
    Join Date
    Dec 2007
    Posts
    1

    i had the same problem

    none of these _SERVER things worked for me either. but this did:

    $joe = apache_request_headers();
    echo $joe['PC-Remote-Addr'];

  12. #12
    Junior Member
    Join Date
    Jan 2008
    Posts
    1
    Hi bradgrafelman

    Am also getting same problem in getting Ipaddress... I tried my internal it (http://192.168...) instead (http://localhost/) bur stil am getting result as 127.0.0.1 like that..

    wat is solution for this..

  13. #13
    Junior Member
    Join Date
    Apr 2008
    Posts
    1

    client IP address

    hi chramya,

    Are you connecting to apache from the same machine were apache is installed?

    If you are doing this, you may check the following:
    from command prompt type command (windows):
    route print
    it shows the route table, which will see something like that:

    Network Destiny Netmask Gateway Interface Metrica
    ....
    172.168.0.7 255.255.255.255 127.0.0.1 127.0.0.1 10
    .....
    default gateway: 192.168.0.1

    if you see a line on your ip have as a gateway 127.0.0.1, your connection to apache goes throught loopback (127.0.0.1).

    Try to connect from other machine or put your web on a server (out of your local network) and try again.

    Ujin

  14. #14
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Quote Originally Posted by trigg12 View Post
    I use this function :




    refer : http://admincmd.blogspot.com
    You saved my life with this one, Trigg!
    I have user the 'REMOTE_ADDR' variable for years on the same server and suddenly, this started to give server address, not the client.
    Does anyone have an idea why this happened? I only tweaked with phptraffic. Maybe it has messed something in the shades?

  15. #15
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    hi
    jeyabala.. try dis..

    $ip= $REMOTE_ADDR;
    echo "<br> Your ip : " .GetHostByName($ip);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •