Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.
- Index
- » Chameleon / ChameleonLite
- » Chameleon
- » Chameleon - PHP to select from an...
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?
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.
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?
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.
Re: Chameleon - PHP to select from an array of regions
- Index
- » Chameleon / ChameleonLite
- » Chameleon
- » Chameleon - PHP to select from an...
Board Info
- Board Stats:
- Total Topics:
- 1698
- Total Polls:
- 6
- Total Posts:
- 5944
- Posts this week:
- 4
- User Info:
- Total Users:
- 5530
- Newest User:
- lawfood2
- Members Online:
- 0
- Guests Online:
- 106
- 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