You are here

function ip_login_login in IP Login 5

Same name and namespace in other branches
  1. 6.2 ip_login.module \ip_login_login()
  2. 6 ip_login.module \ip_login_login()
  3. 7.3 ip_login.module \ip_login_login()
  4. 7.2 ip_login.module \ip_login_login()

Log user in by ip address and profile_ip profile field

Parameters

string $ip ip address:

Return value

void

1 call to ip_login_login()
ip_login_init in ./ip_login.module
Implementation of hook_init

File

./ip_login.module, line 61

Code

function ip_login_login($ip) {

  //get wildcard IP for fuzzy range matches e.g 203.123.456.*
  $ip_range = ip_login_get_range($ip);

  //get profile field for ip address
  $profile_field = variable_get('ip_login_profile_ip_field', 'profile_ip');

  //DB lookup uid with passed ip address or range
  $result = db_fetch_object(db_query("SELECT pv.uid, pv.value FROM {profile_values} pv\n             INNER JOIN {profile_fields} pf ON pv.fid = pf.fid\n             WHERE pf.name = '%s' AND (pv.value = '%s' OR pv.value = '%s')", $profile_field, $ip, $ip_range));

  //if a uid is returned with the passed ip address...
  if ($result) {

    //if can load user from uid where user status is active
    if ($account = user_load(array(
      'uid' => $result->uid,
      'status' => 1,
    ))) {

      //login by assigning account to global $user object
      global $user;
      $user = $account;

      //this part logs the user in
      $message = t('Welcome %name. You are now logged into %sitename.', array(
        '%name' => $user->name,
        '%sitename' => variable_get('site_name', 'this website'),
      ));

      //set welcome message
      drupal_set_message($message);
      $message = t('Session opened for %name.', array(
        '%name' => $user->name,
      ));

      //log login event message
      watchdog('user', $message);

      // Update the user table timestamp noting user has logged in.
      db_query("UPDATE {users} SET login = %d WHERE uid = %d", time(), $user->uid);

      //IMPORTANT: drupal call to regenerate session data for anon user to authenticated user
      sess_regenerate();
    }
  }

  //set processed session flag
  $_SESSION['ip_login_complete'] = 1;
}