最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to expire session after 2 hours and also expire when browser closed?

programmeradmin1浏览0评论

I want to expire session after 2 hours and also want to expire session when browser closed

add_filter('auth_cookie_expiration', 'my_expiration_filter', 99, 3);
function my_expiration_filter($seconds, $user_id, $remember){
    $expiration = 2*60*60; //UPDATE HERE;
    return $expiration;
}

I want to expire session after 2 hours and also want to expire session when browser closed

add_filter('auth_cookie_expiration', 'my_expiration_filter', 99, 3);
function my_expiration_filter($seconds, $user_id, $remember){
    $expiration = 2*60*60; //UPDATE HERE;
    return $expiration;
}
Share Improve this question asked Aug 26, 2019 at 11:20 SudhirSudhir 1136 bronze badges 2
  • 1 If you set the expiration to 2 hours, then the expiration is 2 hours. The only way to end a session when the browser is closed is to set the cookie expiration to 0. You can’t do both. – Jacob Peattie Commented Aug 26, 2019 at 12:08
  • @JacobPeattie How to expire cookie using wordpress – Sudhir Commented Aug 27, 2019 at 9:32
Add a comment  | 

1 Answer 1

Reset to default 2

It is the correct hook. I often also add a bit of javascript, to active the checkboxes inside the form. In your case it would not be necessary to use the $accepted_args parameter – 3 – of the add_filter() hook, because you're not using any parameters, and simply work with a return value as integer, in seconds.

if ( ! function_exists( 'keep_me_logged_in_for_1_year' ) ) {

    add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );
    function keep_me_logged_in_for_1_year() {

        return 31556926; // 1 year in seconds
    }

    add_filter( 'login_footer', 'set_default_true_on_checkbox' );
    function set_default_true_on_checkbox() {
        ?>
        <script type="text/javascript">
        document.getElementById( 'rememberme' ).checked = true;
        document.getElementById( 'wp-submit' ).focus();
        </script>
        <?php
    }

}

If you also want to check for a closed browser, then you need a check via javascript, that js should also be added via the hook login_footer. See this how to to learn more about the idea behind it.

发布评论

评论列表(0)

  1. 暂无评论