Title: Last login script
Last modified: August 22, 2016

---

# Last login script

 *  [mattywilko](https://wordpress.org/support/users/mattywilko/)
 * (@mattywilko)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/last-login-script/)
 * Hi there,
 * I’ve got this script to update a table in my database with the logout time of
   a user, then when they log in next time, this data is shown in the form of “Last
   login: date time”.
 * However this isnt working, and was hoping for some help.
 * Posted in functions.php
 *     ```
       function userloginvarset($login){
           global $user_ID;
           global $userlogininfo;
           $userlogininfo = get_user_by('login',$login);
       }
       add_action('wp_login','userloginvarset');
   
       function last_login($login) {
           update_usermeta($userlogininfo->ID, 'last_login', current_time('mysql'));
       }
   
       add_action('wp_logout','last_login');
   
       function get_last_login($user_id) {
           $last_login = get_user_meta($user_id, 'last_login', true);
           $date_format = get_option('date_format') . ' ' . get_option('time_format');
           $the_last_login = mysql2date($date_format, $last_login, false);
           echo $the_last_login;
       }
       ```
   
 * and
    Posted in page.php
 *     ```
       global $userdata;
                       get_currentuserinfo();
   
                       echo('Welcome '.$userdata->display_name.'!</br>');
                       _e('Last login:');
                       get_last_login($userdata->ID);
       ```
   
 * the displaying of the data works fine, its just the updating of the table. Thanks
   in advance

The topic ‘Last login script’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [mattywilko](https://wordpress.org/support/users/mattywilko/)
 * Last activity: [11 years, 6 months ago](https://wordpress.org/support/topic/last-login-script/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
