SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    153
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Problem finding the users local IP address

    Hello,

    I'm working on a script to pull in the users local IP address so that in turn I can use the ip address to find the users location. I found the following script which should pull in the userslocal IP address but its not working.

    Results from the script below: ::1

    what am I doing wrong?

    PHP Code:
    function get_ip_address() 
    {    foreach (array(
    'HTTP_CLIENT_IP''HTTP_X_FORWARDED_FOR''HTTP_X_FORWARDED''HTTP_X_CLUSTER_CLIENT_IP''HTTP_FORWARDED_FOR''HTTP_FORWARDED''REMOTE_ADDR') as $key
        {    if (
    array_key_exists($key$_SERVER) === true
            {    foreach (
    explode(','$_SERVER[$key]) as $ip
                {    if (
    filter_var($ipFILTER_VALIDATE_IP) !== false
                    {    return 
    $ip;    }
                }
            }
        }


  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,835
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    I hust tried your script both localhost and onlie and it works fine:

    PHP Code:

    function get_ip_address()  
    {
      
    $aa = array
      (
        
    'HTTP_CLIENT_IP'
        
    'HTTP_X_FORWARDED_FOR'
        
    'HTTP_X_FORWARDED'
        
    'HTTP_X_CLUSTER_CLIENT_IP'
        
    'HTTP_FORWARDED_FOR'
        
    'HTTP_FORWARDED'
        
    'REMOTE_ADDR'
      
    );
      foreach ( 
    $aa as $key)  
      {
        if (
    array_key_exists($key$_SERVER) === true)  
        {
          foreach (
    explode(','$_SERVER[$key]) as $ip)  
          {
            if (
    filter_var($ipFILTER_VALIDATE_IP) !== false)  
            {
               return 
    '$_SERVER["' .$key .'"]  = ' .$ip;
            } 
          } 
        } 
      } 
    }
    #endfunc

    echo get_ip_address(); 
    And no doubt you will be delighted to know that Google Webmaster's Tools->Health->Fetch as Google:

    Server address is: $_SERVER["REMOTE_ADDR"] = 66.249.75.36
    Last edited by John_Betong; Apr 11, 2013 at 10:57. Reason: spellin and added $_SERVER $key and GWT results
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    153
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks for the replay back.
    sorry for the confusion, when i copy/pasted the post i forgot to include the call to the function.
    problem still there..



    Quote Originally Posted by John_Betong View Post
    I hust tried your script both loalhost and olie and it works fine:

    PHP Code:

    function get_ip_address()  
    {
        foreach (array(
    'HTTP_CLIENT_IP''HTTP_X_FORWARDED_FOR''HTTP_X_FORWARDED''HTTP_X_CLUSTER_CLIENT_IP''HTTP_FORWARDED_FOR''HTTP_FORWARDED''REMOTE_ADDR') as $key)  
        {
          if (
    array_key_exists($key$_SERVER) === true)  
          {
            foreach (
    explode(','$_SERVER[$key]) as $ip)  
            {
              if (
    filter_var($ipFILTER_VALIDATE_IP) !== false)  
              {    return 
    $ip;    } 
            } 
          } 
        } 
      }

     echo 
    get_ip_address(); 

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Do a var_dump($_SERVER) see what you are getting for those items. Chances are something before REMOTE_ADDR is returning a value.

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    153
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hello cpradio,
    I dont see the ip address anywhere.

    parts of var_dump:
    Code:
    Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 Server at localhost Port 80
    " 
    ["SERVER_SOFTWARE"]=> string(45) "Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7" 
    ["SERVER_NAME"]=> string(9) "localhost" 
    ["SERVER_ADDR"]=> string(3) "::1" 
    ["SERVER_PORT"]=> string(2) "80" 
    ["REMOTE_ADDR"]=> string(3) "::1" 
    ["DOCUMENT_ROOT"]=> string(15) "C:/xampp/htdocs" 
    ["REQUEST_SCHEME"]=> string(4) "http" 
    ["CONTEXT_PREFIX"]=> string(0) "" 
    ["CONTEXT_DOCUMENT_ROOT"]=> string(15) "C:/xampp/htdocs" 
    ["SERVER_ADMIN"]=> string(20) "postmaster@localhost" 
    ["SCRIPT_FILENAME"]=> string(40) "C:/xampp/htdocs/geocoding/geotesting.php" 
    ["REMOTE_PORT"]=> string(5) "53878" 
    ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" 
    ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" 
    ["REQUEST_METHOD"]=> string(4) "POST" 
    ["QUERY_STRING"]=> string(0) "" 
    ["REQUEST_URI"]=> string(25) "/geocoding/geotesting.php" 
    ["SCRIPT_NAME"]=> string(25) "/geocoding/geotesting.php" 
    ["PHP_SELF"]=> string(25) "/geocoding/geotesting.php" 
    ["REQUEST_TIME_FLOAT"]=> float(1365702806.399) 
    ["REQUEST_TIME"]=> int(1365702806) }

    Quote Originally Posted by cpradio View Post
    Do a var_dump($_SERVER) see what you are getting for those items. Chances are something before REMOTE_ADDR is returning a value.

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    153
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Im using XAMPP to test my script.. could xampp be the cause of the problem?

  7. #7
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,835
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Please format your script using PHP tags:

    also I have this function which formats array output:


    PHP Code:

    //----------------------
    //
    //  typical usage:
    //           vd( $_SERVER );
    // 
    //----------------------
    function vd$a=array(), $lVarDump=FALSE )
    {
      echo 
    '<pre>';
        if(
    $lVarDump):
         
    var_dump($a);
       else:
         
    print_r($a);
       endif;
      echo 
    '</pre>'
    Fron your var_dump($_SERVER) results ["REMOTE_ADDR"]=> string(3) "::1"

    so the function is reporting correctly.

    Have you tested the script online?

    I am using XAMPP Lite.
    Last edited by John_Betong; Apr 11, 2013 at 11:19. Reason: added REMOTE_ADDR and modified function
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You have apache setup to listen to IPv6, which ::1 means local, which is correct if you are running this local.

    To configure apache to ONLY listen to IPv4, see this thread: http://stackoverflow.com/questions/2...g-ipv4-address

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Alright, I'm going to lay some things down. ONLY use "REMOTE_ADDR" to get a user's IP (except when your server is behind a known proxy). Use NOTHING else.

    This is all your get_ip_address function should do:
    PHP Code:
    function get_ip_address () { return $_SERVER['REMOTE_ADDR']; } 
    That is all.

    Now, when you are accessing the script locally via localhost the IP will always be either ::1 or 127.0.0.1 ALWAYS.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

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