What it does
Auto-detects which country user is from and auto-fills the location field on the registration form using their IP Address.
Installing
Import the 'ip2country' table attached into your forums database. Either use phpMyAdmin or from ssh.
(Make sure your mysql max_packet size is large enough to handle the ~2 Mb file -- otherwise try downloading this split table here)
Open up register.php and look for:
PHP Code:
while ($profilefield=$DB_site->fetch_array($profilefields)) {
$profilefieldname = "field$profilefield[profilefieldid]";
Above that place the following:
PHP Code:
// CUSTOM ADD AUTO LOCATION
$ip_number = sprintf("%u",ip2long($REMOTE_ADDR));
$ct_array = $DB_site->query_first("SELECT COUNTRY_NAME FROM ip2country WHERE $ip_number >= IP_FROM AND $ip_number <= IP_TO LIMIT 1");
$bbuserinfo['field2'] = ucwords(strtolower($ct_array['COUNTRY_NAME']));
// END CUSTOM ADD AUTO LOCATION
Once you've done that, you're all set.
Show Your Support
This modification may not be copied, reproduced or published elsewhere without author's permission.
I made a small change to the code Tim Wheatley posted for the "member.php" file as follows..
Add:
PHP Code:
// CUSTOM ADD AUTO LOCATION
$ip_number = sprintf("%u",ip2long($REMOTE_ADDR));
$ct_array = $DB_site->query_first("SELECT COUNTRY_NAME FROM ip2country WHERE $ip_number >= IP_FROM AND $ip_number <= IP_TO LIMIT 1");
if (!$bbuserinfo['field2']) {
$bbuserinfo['field2'] = ucwords(strtolower($ct_array['COUNTRY_NAME']));
}
// END CUSTOM ADD AUTO LOCATION
Above:
PHP Code:
while ($profilefield=$DB_site->fetch_array($profilefields)) {
$profilefieldname="field$profilefield[profilefieldid]";
With the code Tim had posted, the location would be automatically changed to just the country whenever a user modified their profile. With the change I made though, the location will only be automatically changed if the location field is empty (i.e. the user hasn't filled in a location manually).
In other words, if the user already has something in the location field it won't be changed. If the location field is empty, it will be automatically filled in with the country name.
I also made the following change so no user can leave the location field empty in their profile..
In "root/member.php" find:
PHP Code:
// check extra profile fields
$userfields="";
$profilefields=$DB_site->query("SELECT profilefieldid,required,title,size,maxlength
FROM profilefield
WHERE editable = 1");
And Add Below It:
PHP Code:
// CUSTOM ADD AUTO LOCATION
if (!$field2) {
$ip_number = sprintf("%u",ip2long($REMOTE_ADDR));
$ct_array = $DB_site->query_first("SELECT COUNTRY_NAME FROM ip2country WHERE $ip_number >= IP_FROM AND $ip_number <= IP_TO LIMIT 1");
$field2 = ucwords(strtolower($ct_array['COUNTRY_NAME']));
}
// END CUSTOM ADD AUTO LOCATION
In "root/register.php" find:
PHP Code:
// check extra profile fields
$userfields="";
$userfieldsnames="(userid";
$profilefields=$DB_site->query("SELECT maxlength,profilefieldid,required,title
FROM profilefield
WHERE editable = 1
ORDER BY displayorder");
And Add Below It:
PHP Code:
// CUSTOM ADD AUTO LOCATION
if (!$field2) {
$ip_number = sprintf("%u",ip2long($REMOTE_ADDR));
$ct_array = $DB_site->query_first("SELECT COUNTRY_NAME FROM ip2country WHERE $ip_number >= IP_FROM AND $ip_number <= IP_TO LIMIT 1");
$field2 = ucwords(strtolower($ct_array['COUNTRY_NAME']));
}
// END CUSTOM ADD AUTO LOCATION
With these additions, if a user leaves the location field empty (either when registering or updating their profile), the country will be automatically inserted into the database. Resulting in no empty locations.