0

I some working code in hook_user_login() that awards 5 points to users for each day they log into the site. If they log in twice in one day, points are not awarded the second time.

Originally, I thought users would login each time they visit the site but since users stay logged in between visits, they are not getting points for visiting once per day. For example, if they log in today, they get 5 points but tomorrow and each day they visit thereafter, they don't receive any.

I would like users to be awarded for each day they visit the site whether they need to log in or not. I wasn't able to find another hook that could do it. Does anyone know of one or some other method of tracking user visits?

4

You could store the last updated timestamp in the session on login, and then check it on each page load to see if it's within your parameters:

function MYMODULE_init() {
  // Only proceed if there's a timestamp in the session. Otherwise defer to
  // hook_user_login() to take care of setting that.
  if (user_is_logged_in() && isset($_SESSION['MYMODULE_point_check_timestamp']) {
    // If the timestamp is more than 24 hours ago, award more points.
    if (REQUEST_TIME - $_SESSION['MYMODULE_point_check_timestamp'] > (60 * 60 * 24)) {
      award_points();
    }
  }
}

function MYMODULE_user_login(&$edit, $account) {
  award_points();

  // Track the time points were awarded.
  $_SESSION['MYMODULE_point_check_timestamp'] = REQUEST_TIME;
}

function MYMODULE_user_logout($account) {
  // Clean up.
  if (isset($_SESSION['MYMODULE_point_check_timestamp'])) {
    unset($_SESSION['MYMODULE_point_check_timestamp']);
  }
}
  • Thank you for your help again Clive! This is not the first time you've answered one of my questions. The code sample was especially useful. Since points are awarded for each day and not 24-hour period, I used "intval(date('Ymd', REQUEST_TIME))" to convert the timestamps into discrete days for comparison's sake if it helps anyone who sees this. – user320691 Jun 5 '13 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.