Link to home
Start Free TrialLog in
Avatar of Sabrin
Sabrin

asked on

help implement code

hello,
this is what im using to block ips from china and korea on my site

<?
include("/usr/local/etc/geoip.inc");
$gi = geoip_open("/usr/local/etc/GeoIP.dat",GEOIP_STANDARD);
$IP_Addr = $HTTP_SERVER_VARS["REMOTE_ADDR"];

if (geoip_country_code_by_addr($gi, $IP_Addr) == CN){
  print "no access allowed";
}else if (geoip_country_code_by_addr($gi, $IP_Addr) == KR){
  print "no access allowed";
}

geoip_close($gi);

?>


I need help implementing this code! is so if they are using a us proxy they can get in!  

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"];
} else {
 if ($_SERVER["HTTP_CLIENT_IP"]) {
   $ip = $_SERVER["HTTP_CLIENT_IP"];
 } else {
   $ip = $_SERVER["REMOTE_ADDR"];
 }
}

Avatar of TeRReF
TeRReF
Flag of Netherlands image

How about:

<?

include("/usr/local/etc/geoip.inc");

$access = false;
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"];
 // proxy, so access allowed
 $access = true;
} else {
 if ($_SERVER["HTTP_CLIENT_IP"]) {
   $ip = $_SERVER["HTTP_CLIENT_IP"];
 } else {
   $ip = $_SERVER["REMOTE_ADDR"];
 }
}

// access still false, so geoip test needed
if ($access === false) {
 $gi = geoip_open("/usr/local/etc/GeoIP.dat",GEOIP_STANDARD);
 $IP_Addr = $HTTP_SERVER_VARS["REMOTE_ADDR"];

 if (geoip_country_code_by_addr($gi, $IP_Addr) == CN){
  print "no access allowed";
 }else if (geoip_country_code_by_addr($gi, $IP_Addr) == KR){
  print "no access allowed";
 }

 geoip_close($gi);
// proxy, access allowed
} else {
  print "access allowed";
}

?>
Another thing, you should change this line:
$HTTP_SERVER_VARS["REMOTE_ADDR"];
into
$_SERVER["REMOTE_ADDR"];

$HTTP_SERVER_VARS["REMOTE_ADDR"]; is depreciated and should not be used anymore (if you ever upgrade PHP it will save you some headaches :)
Avatar of Sabrin
Sabrin

ASKER

thanks guys, this is my php-4.3.9-3.18
$HTTP_SERVER_VARS will work in PHP4, but not anymore in PHP5 (per default). So, it's not necessary to browse through the code right away to change it, but if you ever come accross line of code with $HTTP_SERVER_VARS, you should make it a habbit to change it.
Avatar of Sabrin

ASKER

so If someone from china is using ip 1.2.3.4
and connects to a proxy from usa 4.5.6.7
and goes to the site, he will be blocked?
this is what I want!
Avatar of Sabrin

ASKER

I dont want to block proxy traffic maybe theres a guy from canada
using a proxy in the US i dont want to block him
Avatar of Sabrin

ASKER

something like if user is using proxy check real ip
if its from china dont let him in
if user is not using proxy and ip is from china
dont let him in

Avatar of Sabrin

ASKER

the geo ip will make sure the ip is from x country and do what I tell!
> so if they are using a us proxy they can get in!  

Sorry, I thought that anyone using a proxy was allowed in :)

I'll have a quick bite and then change the code.

ASKER CERTIFIED SOLUTION
Avatar of TeRReF
TeRReF
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sabrin

ASKER

sweet thanks a lot man!
You're welcome :)