Skip to content

Instantly share code, notes, and snippets.

@luxixing
Created October 16, 2013 09:33
Show Gist options
  • Save luxixing/7005165 to your computer and use it in GitHub Desktop.
Save luxixing/7005165 to your computer and use it in GitHub Desktop.
php ip
<?php
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
{
if ($_SERVER["HTTP_CLIENT_IP"])
{
$proxy = $_SERVER["HTTP_CLIENT_IP"];
} else {
$proxy = $_SERVER["REMOTE_ADDR"];
}
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
echo $ip . "\n";
} else {
if ($_SERVER["HTTP_CLIENT_IP"])
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
}
echo "Your IP $ip";
if (isset($proxy))
{
echo "Your proxy IP is $proxy";
}
@xeoncross
Copy link

This is trusting the user agents to not lie to you. ALWAYS capture the REMOTE_ADDR - if you want to save the proxy info also then that's great but don't skip REMOTE_ADDR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment