Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.

helpme

 

Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.

 

Chameleon - PHP to select from an array of regions

Chameleon - PHP to select from an array of regions

Hi There, I'm using Chameleon and I am very happy with it so far. I'm stumped though on how to create a correct geoip php to do the following:

Rule to:
Determine which country and state the user is from.
Assign user to VM shopper group based on state "US East" and "US West"

Ive cobbled the following together form the example you've given, but no success.

Heres what I'm starting with:


$country = $geoip->country;
$region = $geoip->mostSpecificSubdivision;

if ($country != 'United States') return true;

if ($region == 'California' or $region == 'Utah') return true;



I have "GeoLite2 City" selected and up to date

I'm stumped, can you help?

metabern
Beginner Modder
ranks
useravatar
Offline
18 Posts
Administrator has disabled public posting

Re: Chameleon - PHP to select from an array of regions

Hi,

you're not far off getting this working.

$country = $geoip->country;  // yes
$region = $geoip->mostSpecificSubdivision->name; // just needed the "->name" on the end
if ($country != 'United States') return false;  // "true" makes this part of the rule succeed. is that what you wanted? I'm guessing you probably want "false" here, so that if they are not from the West Coast of the USA the rule will fail.
if ($region == 'California' or $region == 'Utah') return true;  // yes, rule succeeds for these ones.
return false; // everywhere else in the USA is a fail.

So in this rule you're trying to make the rule succeed if (and only if) they are from the west coast. So if they are not from the USA, it's an automatic "false" ("Fail" action).
If they are from either of the 2 states mentioned, the rule succeeds. You can add in all the other west coast states too.

In the next rule, you can make it almost identical except searching for all the East Coast states.

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Re: Chameleon - PHP to select from an array of regions

You are correct about what I am trying to do. I copied the following into my php:


$country = $geoip->country; // yes
$region = $geoip->mostSpecificSubdivision->name; // just needed the "->name" on the end
if ($country != 'United States') return false; // "true" makes this part of the rule succeed. is that what you wanted? I'm guessing you probably want "false" here, so that if they are not from the West Coast of the USA the rule will fail.
if ($region == 'California' or $region == 'Utah') return true; // yes, rule succeeds for these ones.
return false; // everywhere else in the USA is a fail

Using https://geopeeker.com/, I ran a remote test on US West California and it generated a php fail

I'm not sure if its a real fail or not, how can I attach a screenshot of the debug report?

metabern
Beginner Modder
ranks
useravatar
Offline
18 Posts
Administrator has disabled public posting

Re: Chameleon - PHP to select from an array of regions

Hi,

One way to debug this is to put the following code at the beginning of the PHP rule. The output will mess up your page (temporarily) but at least it tells you what the $geoip object has been able to determine about the IP address.

Code:

echo "<i>" . '$geoip->city->name = </i>' . @$geoip->city->name . "<br />";

echo "<i>" . '$geoip->city->confidence = </i>' . @$geoip->city->confidence . "<br />";
echo "<i>" . '$geoip->postal->code = </i>' . @$geoip->postal->code . "<br />";
echo "<i>" . '$geoip->postal->confidence = </i>' . @$geoip->postal->confidence . "<br />";
echo "<i>" . '$geoip->country->name = </i>' . @$geoip->country->name . "<br />";
echo "<i>" . '$geoip->country->isoCode = </i>' . @$geoip->country->isoCode . "<br />";
echo "<i>" . '$geoip->continent->names[\'en\'] = </i>' . @$geoip->continent->names['en'] . "<br />";
echo "<i>" . '$geoip->continent->code = </i>' . @$geoip->continent->code . "<br />";
echo "<i>" . '$geoip->registeredCountry->names[\'en\'] = </i>' . @$geoip->registeredCountry->names['en'] . "<br />";
echo "<i>" . '$geoip->registeredCountry->isoCode = </i>' . @$geoip->registeredCountry->isoCode . "<br />";
for ($i = 0; $i < count(@$geoip->subdivisions); $i++) {
    echo "<i>" . '$geoip->subdivisions[' . $i . ']->name = </i>' . @$geoip->subdivisions[$i]->name . "<br />";
    echo "<i>" . '$geoip->subdivisions[' . $i . ']->isoCode = </i>' . @$geoip->subdivisions[$i]->isoCode . "<br />";
}
echo "<i>" . '$geoip->mostSpecificSubdivision->name = </i>' . @$geoip->mostSpecificSubdivision->name . "<br />";
echo "<i>" . '$geoip->mostSpecificSubdivision->isoCode = </i>' . @$geoip->mostSpecificSubdivision->isoCode . "<br />";
echo "<i>" . '$geoip->location->timeZone = </i>' . @$geoip->location->timeZone . "<br />";
echo "<i>" . '$geoip->location->accuracyRadius = </i>' . @$geoip->location->accuracyRadius . "<br />";
echo "<i>" . '$geoip->location->latitude = </i>' . @$geoip->location->latitude . "<br />";
echo "<i>" . '$geoip->location->longitude = </i>' . @$geoip->location->longitude . "<br />";
echo "<i>" . '$geoip->location->metroCode = </i>' . @$geoip->location->metroCode . "<br />";
echo "<i>" . '$geoip->location->populationDensity = </i>' . @$geoip->location->populationDensity . "<br />";
echo "<i>" . '$geoip->location->averageIncome = </i>' . @$geoip->location->averageIncome . "<br />";

I am guessing you may find that for your IP address, the state has not been detected. Geo IP detection is not an exact science, and a certain proportion of IP addresses are not resolved correctly.
I'll be interested in what result you get from this.

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Re: Chameleon - PHP to select from an array of regions

I emailed you the screen shot of the remote login with Chameleon debug on. It shows California as the state we want a "true" for

metabern
Beginner Modder
ranks
useravatar
Offline
18 Posts
Administrator has disabled public posting

Re: Chameleon - PHP to select from an array of regions

oh, I think I spotted it: the first line should be:

$country = $geoip->country->name;

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
1679
Total Polls:
6
Total Posts:
5913
Dormant:
User Info:
 
Total Users:
10778
Newest User:
negina4qx
Members Online:
0
Guests Online:
178

Online: 
There are no members online

Forum Legend:

 Topic
 New
 Locked
 Sticky
 Active
 New/Active
 New/Locked
 New Sticky
 Locked/Active
 Active/Sticky
 Sticky/Locked
 Sticky/Active/Locked