0

I have a method that logs the users login time.

public function log_login() {

    if ( isset($_COOKIE['current_visit']) ) {
        return;
    }

    // Get current user
    $user = wp_get_current_user();
    $login = (array) get_user_meta( $user->ID, 'last_login', true );

    // Save only the last 5 logins
    $login = array_slice( $login, -5 );

    if(empty($login) || ! is_array($login)){
        $login = array(time());
    }
    else{
        if(count($login) == 10){
             array_shift($login);
        }
        $login[] = time();
    }

    update_user_meta( $user->ID, 'last_login', $login);

    setcookie('current_visit', time(), time()+60 ); // expire in 1 minute for testing purposes.
}

So on my local install I was getting an array with a timestamp a second apart so something like array(6) { [0]=> int(1448035156) [1]=> int(1448035157)... This is definitely not what I am trying to do. I read that 404's could be the cause so I looked and had one on my local instance. I fixed the 404 and the duplicate stopped. On a live I am still getting duplicate logs, but there are no 404's. Does anyone know of other solutions to this problem?

  • You mentioned you are getting duplicate logs, but there is no logging mechanism in the code you posted, can you explain? – Tom J Nowell Nov 20 '15 at 23:46
  • The time is logged within the user meta 'last_login' – Juan Rangel Nov 20 '15 at 23:54
  • If you're sure that the method isn't being called twice I would check the header responses you're getting on your site, a redirect could be causing this perhaps? – Craig Pearson Nov 21 '15 at 2:29

Your Answer

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

Browse other questions tagged or ask your own question.