Forum Moderators: coopster

Message Too Old, No Replies

Correct syntax for multiple variables

         

Mokita

6:02 am on Apr 1, 2006 (gmt 0)

10+ Year Member



I'm trying to set up Maxmind GeoIP to restrict access to one particular page, to visitors from only a few countries.

So far, this part of the code is working well allowing one country:

if ($visitor_country_name == "France") {
include("index.html");
} else {
include("sorry.html");
}

But I want to allow more countries. Can I just add them, separated by a comma as in the following example, or is there some other correct syntax?

if ($visitor_country_name == "France, Belgium, Germany") {
include("index.html");
} else {
include("sorry.html");
}

TIA

jatar_k

6:11 am on Apr 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could build them into an array an then use in_array [php.net]

$badctryarr = array('France', 'Belgium', 'Germany');
if (in_array($visitor_country_name,$badctryarr)) {
etc

Habtom

8:56 am on Apr 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OR you can just list them

if ($visitor_country_name == "France" OR $visitor_country_name == "Belgium" OR $visitor_country_name == "Germany") {
include("index.html");
} else {
include("sorry.html");
}

Array is better though, if you can understand them.

Habtom

Mokita

9:47 pm on Apr 1, 2006 (gmt 0)

10+ Year Member



Many thanks for the replies! :)

I've used the array as it is tidier.