Automatic signout when inactive
-
Hi
I?m using this code (from an old not maintained plugin) for logout inactive users:
// -----------------------HOOKS-------------------------------- add_action('wp_login', 'automatic_sign_out_update_last_activity_on_login_event', 1); add_action('get_header', 'automatic_sign_out_inactivity_event', 1); add_action('admin_init', 'automatic_sign_out_inactivity_event', 1); // Stores time of last activity into cookie define('AUTOMATIC_LOG_OUT_LAST_ACTIVITY_TIME', 'bry_time'); //-----Redirect User to this page on Automatic Log Out------------ /*Adjust this variable to change the location to where the logged out user will be redirected to. If you don't want to redirect the user anywhere, then leave it as site_url() */ //define('AUTOMATIC_SIGN_OUT_REDIRECT_ADDRESS', site_url()); define('AUTOMATIC_SIGN_OUT_REDIRECT_ADDRESS', "https://www.test.se/login"); //-----Maximum time allowed before logged out for inactivity---------- /*Adjust this variable to change the maximum time. The time unit is seconds. This means that 60*60*24 is equal to 1 day. Why? because 60 seconds * 60 minutes * 24 hours = 1 day */ //define('AUTOMATIC_SIGN_OUT_MAXIMUM_INACTIVITIY_TIME', 60*60*24); // 1 day //define('AUTOMATIC_SIGN_OUT_MAXIMUM_INACTIVITIY_TIME', 60*60); // 1 hour define('AUTOMATIC_SIGN_OUT_MAXIMUM_INACTIVITIY_TIME', 60*20); // 20 min //define('AUTOMATIC_SIGN_OUT_MAXIMUM_INACTIVITIY_TIME', 60); // 1 minute //define('AUTOMATIC_SIGN_OUT_MAXIMUM_INACTIVITIY_TIME', 20); // 20 sec only for testing //------------------------------------------------------------------- /* This function logs the user out then immediately redirects them to the specified page. If no page is specified, then they will just be logged out and won't be redirected anywhere */ function automatic_signout_perform_logout_event() { wp_logout(); wp_redirect(AUTOMATIC_SIGN_OUT_REDIRECT_ADDRESS); } function automatic_sign_out_activate() { automatic_sign_out_update_last_activity_event(); } function automatic_sign_out_inactivity_event() { if (is_user_logged_in()) { $last_activity_time = AUTO_SIGN_OUT_GET_TIME_OF_LAST_ACTIVITY(); if ($last_activity_time + AUTOMATIC_SIGN_OUT_MAXIMUM_INACTIVITIY_TIME < time()) { // log out automatic_signout_perform_logout_event(); }else { // Stay logged in and update cookie automatic_sign_out_update_last_activity_event(); } } } // This function gets the last time of activity function AUTO_SIGN_OUT_GET_TIME_OF_LAST_ACTIVITY() { if (is_user_logged_in()) { return (int) get_usermeta(get_current_user_id(), AUTOMATIC_LOG_OUT_LAST_ACTIVITY_TIME); }else { return 0; } } // This function sets the last active time on login. function automatic_sign_out_update_last_activity_on_login_event($username) { $now = time(); try { $user_id = get_userdatabylogin($username)->ID; if ($user_id != null && $user_id > 0) { update_usermeta($user_id, AUTOMATIC_LOG_OUT_LAST_ACTIVITY_TIME, $now); } }catch (Exception $ex) { } } function automatic_sign_out_update_last_activity_event() { if (is_user_logged_in()) { $now = time(); update_usermeta(get_current_user_id(), AUTOMATIC_LOG_OUT_LAST_ACTIVITY_TIME, $now); } } register_activation_hook( __FILE__, 'automatic_sign_out_activate' );
Now I want to add in a message when logout happens, like “Due to inactivity please login again”.
Any idea if its possible to solve this and how?
Per
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Automatic signout when inactive’ is closed to new replies.