Link to home
Start Free TrialLog in
Avatar of Sabrin
Sabrin

asked on

less code

hello,
is there a way of doing this with less code!!

if (geoip_country_code_by_addr($gi, $ip) == CN){
  exit;
}else if (geoip_country_code_by_addr($gi, $ip) == RU){
  exit;
}else if (geoip_country_code_by_addr($gi, $ip) == SK){
  exit;
}else if (geoip_country_code_by_addr($gi, $ip) == TR){
  exit;
}else if (geoip_country_code_by_addr($gi, $ip) == YE){
  exit;
}

I have around 50 country codes!
Avatar of Roonaan
Roonaan
Flag of Netherlands image

if(in_array(geoip_country_code_by_addr($gi, $ip), array('CN', 'RU','SK','TR','YE')) {
  exit();
}
Avatar of Sabrin
Sabrin

ASKER

didnt work
Another way is:

switch(geoip_country_code_by_addr($gi, $ip))
{
    case 'CN':
    case 'RU':
    case 'SK':
    case 'TR':
    case 'YE':
         exit;
    break;
}

Roonaan's way is better, unless you need to do different things for different codes.
are CN/RU/SK/TR/YE strings or constants?
Might need to be something different if they where constants

if(in_array(geoip_country_code_by_addr($gi, $ip), array(CN,RU,SK,TR,YE)) {
  exit();
}

-r-
Avatar of Sabrin

ASKER

i dont know but its not working
>> didnt work

Any more details as to why it didn't work? any errors for example?
Avatar of Sabrin

ASKER

no errors no nothing,
but when I use the code that works I get this

Notice: Use of undefined constant
Avatar of Sabrin

ASKER

Notice: Use of undefined constant US - assumed 'US' in /home/file.php on line 25


line 25 = if (geoip_country_code_by_addr($gi, $ip) == US){
Avatar of Sabrin

ASKER

like this i dont get any notices
if (geoip_country_code_by_addr($gi, $ip) == 'US'){
Avatar of Sabrin

ASKER

this is working

if (geoip_country_code_by_addr($gi, $ip) == 'CN'||'US'){
Avatar of Sabrin

ASKER

what im trying here is that if its (country code) exit and dont show the site
What's the output of  geoip_country_code_by_addr($gi, $ip) ?

Also, what's the output of:
   echo CN;

The specific values might be causing problems.
Avatar of Sabrin

ASKER

what output? theres no output!

GeoIP.dat its just a database of IPs
geoip.inc has all the country codes with numbers

$gi checks to see if $ip is from a country lets say US and if its from US then its not allowed!

if (geoip_country_code_by_addr($gi, $ip) == 'CN'){
 echo "not allowed";
  exit;
}
County codes are strings, and Roonan's code should work.

Also, "Notice: Use of undefined constant" is an error.  In your original code, there were no quotes around the country codes.
What does the below code give you? The in_array should work fine syntactically, but reraly depends on the output of your geoip function.

echo '['.geoip_country_code_by_addr($gi, $ip).']';

-r-
Avatar of Sabrin

ASKER

echo '['.geoip_country_code_by_addr($gi, $ip).']';

[US]
Avatar of Sabrin

ASKER

sorry this doesnt work correctly

if (geoip_country_code_by_addr($gi, $ip) == 'CN'||'US'){
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
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