• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

( 500 Points ) Get user's I.P & or Proxy + Explain Code

Hi Experts,

I have seen lots of snippets out there that you can use to show a users
i.p address, but none of them explain any of the code.

So could someone please post a solid solution that will detect the user's i.p
or proxy and explain each line of the code,

thanks guys
0
babyboy808
Asked:
babyboy808
  • 6
  • 4
  • 2
  • +1
2 Solutions
 
rockiroadsCommented:
what language? Ive got one somewhere in VB/VBA, need to dig it out

If u want ASP, u can dump the server variables, until you find the index for the one u want
http://www.aspfaq.com/show.asp?id=2012

0
 
sivic82Commented:
what programming language?
0
 
babyboy808Author Commented:
Sorry PHP
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rockiroadsCommented:
ok, dont know about the PHP side
quick search, I found this, looks fairly straight forward - its a one liner http://www.alt-php-faq.org/local/24/

0
 
rockiroadsCommented:
It seems all u need to do is check the environment variables.

Expansion on the previous post

<?php
/**
* Call as: $userp = GetUserIP();
*/
function GetUserIP()
{
    $ip = "";

    if (isset($_SERVER))
    {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
        {
            $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }
        elseif (isset($_SERVER["HTTP_CLIENT_IP"]))
        {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        else
        {
            $ip = $_SERVER["REMOTE_ADDR"];
        }
    }
    else
    {
        if ( getenv( 'HTTP_X_FORWARDED_FOR' ) )
        {
            $ip = getenv( 'HTTP_X_FORWARDED_FOR' );
        }
        elseif ( getenv( 'HTTP_CLIENT_IP' ) )
        {
            $ip = getenv( 'HTTP_CLIENT_IP' );
        }
        else
        {
            $ip = getenv( 'REMOTE_ADDR' );
        }
    }
    return $ip;
}
?>

(taken from http://www.oxyscripts.com/itemdisplay.php?id=1059&code=yes)


for a list of environment variables - see this
http://www.php.net/reserved.variables 


0
 
babyboy808Author Commented:
thanks for the reply, but that doesn't show if the user is behind a proxy etc...
And i need the code to be explained...
0
 
rockiroadsCommented:
no, my suggestion does not show the proxy. You did ask though to get the IP or proxy.
Regardless of that, I dont really know PHP so I cannot give you explanations. What I can simply do is redirect you to some sites that may offer this info for you, this is in addition to waiting for a PHP expert to answer your question.
See this - http://uk.php.net/getenv  - there is a mention to get the real IP address - which is what u want.

0
 
rockiroadsCommented:
If u need the attention of a PHP expert, may I suggest you create a post in that section
http://www.experts-exchange.com/Web/Web_Languages/PHP/
For this particular question, u could create a 20ptr which a link to this question, so the PHP experts are aware

ok, some advice for you as well - see this post http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21702263.html

0
 
fostejoCommented:
babyboy808,

the simplest script is probably along the lines of:

<?php
if(isset($HTTP_X_FORWARDED_FOR))  // is the HTTP_X_FORWARDED_FOR variable defined already?
{
    if ($HTTP_X_FORWARDED_FOR)    // A proxy WAS detected..
        {
      ?>
      Proxy Detected.
                Your actual IP address: <?= $HTTP_X_FORWARDED_FOR ?>
      Your Proxy Server: <?= $HTTP_VIA ?>
                Your Proxy I.P address: <?= $REMOTE_ADDR ?>
      <?
        }
}
else   // no proxy was detected
    {
        ?>
        NO Proxy detected - Your Actual IP is: <?= $REMOTE_ADDR ?>
        <?
    }
?>


cheers,
0
 
babyboy808Author Commented:
No luck, with that script fostejo, it outputs

"NO Proxy detected - Your Actual IP is: "

but no address
0
 
babyboy808Author Commented:
Ok, I have found some code and seems to work all I need now is for someone
to explain each piece, thanks

<?php

if (getenv(HTTP_X_FORWARDED_FOR))
{
  $ip=getenv(HTTP_X_FORWARDED_FOR);
    echo $ip;
}
else
if (getenv(HTTP_CLIENT_IP))
{
  $ip=getenv(HTTP_CLIENT_IP);
    echo $ip;
}
else
{
  $ip=getenv(REMOTE_ADDR);
  echo $ip;
}

?>
0
 
fostejoCommented:
babyboy808,

? Strange, the code I posted definately works (for me anyway with IE ;)  - try it at http://www.johnnyfoster.com/Proxytest.php

But, anyway, commenting your script:


if (getenv(HTTP_X_FORWARDED_FOR))           // Is HTTP_X_FORWARDED_FOR already defined - if yes, execute the code in between the next { and }
{
  $ip=getenv(HTTP_X_FORWARDED_FOR);      // set a variable called 'ip' to the current value of 'HTTP_X_FORWARDED_FOR'
    echo $ip;                                                  // ... and print it out to screen
}
else                                                              // HTTP_X_FORWARDED_FOR wasn't already defined so this next code is executed instead
if (getenv(HTTP_CLIENT_IP))                         // If HTTP_CLIENT_IP is already defined then execute the code between the next { and }
{
  $ip=getenv(HTTP_CLIENT_IP);                    // set a variable called 'ip' to the current value of 'HTTP_CLIENT_IP'
    echo $ip;                                                 // ... and print it out to screen
}
else                                                            // HTTP_CLIENT_IP Wasn't already defined so this next block is executed instead
{
  $ip=getenv(REMOTE_ADDR);                     // set a variable called 'ip' to the current value of 'REMOTE_ADDR'
  echo $ip;                                                  // ... and print it out to screen


hope that helps..
0
 
rockiroadsCommented:
babyboy808, did u actually try the script I got and posted that was taken from oxyscripts?
The 2nd part of that is more or less the same as what u have done, only difference with oxyscripts is it checks for certain variables set and uses $_SERVER first, if that is set

$_SERVER refers to server sided variables

also the link to the reserved variables gives an explanation of the variables itself
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now