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

I want to expire session after 2 hours and also want to expire session when browser closedadd_filter('auth_cookie_e

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745211599a4616874.html

相关推荐

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

    I want to expire session after 2 hours and also want to expire session when browser closedadd_filter('auth_cookie_e

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信