You are here

function ip_tracker_user_save in IP address manager 7

Same name and namespace in other branches
  1. 6 ip.module \ip_tracker_user_save()
  2. 7.2 ip.module \ip_tracker_user_save()

Save a record into the ip_tracker table. @NOTE: Maybe you want to check the user uid vs $uid before calling this function.

2 calls to ip_tracker_user_save()
ip_user_insert in ./ip.module
Implements hook_user_insert().
ip_user_login in ./ip.module
Implements hook_user_login().

File

./ip.module, line 119
IP address manager module.

Code

function ip_tracker_user_save($uid) {
  $iplong = ip2long(ip_address());
  if (!empty($iplong)) {

    // Check to see if a row exists for this uid/ip combination.
    $sql = "SELECT visits FROM {ip_tracker} WHERE uid = :uid AND ip = :ip";
    $args = array(
      ':uid' => $uid,
      ':ip' => $iplong,
    );
    $visits = db_query($sql, $args)
      ->fetchField();
    if ($visits) {

      // Update.
      return db_update('ip_tracker')
        ->fields(array(
        'visits' => $visits + 1,
        'last_visit' => REQUEST_TIME,
      ))
        ->condition('uid', $uid)
        ->condition('ip', $iplong)
        ->execute();
    }
    else {

      // Insert.
      return db_insert('ip_tracker')
        ->fields(array(
        'uid' => $uid,
        'ip' => $iplong,
        'visits' => 1,
        'first_visit' => REQUEST_TIME,
        'last_visit' => REQUEST_TIME,
      ))
        ->execute();
    }
  }
}