You are here

function ip2country_user in IP-based Determination of a Visitor's Country 6

Implements hook_user().

Detects IP and determines country upon user login.

File

./ip2country.module, line 127
Determination of user's Country based on IP.

Code

function ip2country_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'login':

      // Successful login. First determine user's country based on IP.
      $ip = ip_address();
      $country_code = ip2country_get_country($ip);

      // Now check to see if this user has "administer ip2country" permission
      // and if debug mode set. If both are TRUE, use debug information
      // instead of real information.
      if (user_access('administer ip2country') && variable_get('ip2country_debug', FALSE)) {
        $type = variable_get('ip2country_test_type', 0);
        if ($type == 0) {

          // Debug Country entered.
          $country_code = variable_get('ip2country_test_country', 'US');
        }
        else {

          // Debug IP entered.
          $ip = variable_get('ip2country_test_ip_address', $ip);
          $country_code = ip2country_get_country($ip);
        }
        drupal_set_message(t('Using DEBUG value for Country - @country', array(
          '@country' => $country_code,
        )));
      }

      // Finally, save country, if it has been determined.
      if ($country_code) {

        // Store the ISO country code in the $user object.
        user_save($account, array(
          'country_iso_code_2' => $country_code,
        ));
      }
      break;
  }
}