简体   繁体   中英

Call 'wp_logout' when cookie expires (php or javascript)

I am looking for a way to call wp_logout (server side) if my wordpress_logged_in_HASH cookie expires. Unfortunalty I can't find any solution so far. Did a lot of research on stackoverflow and google, but still not done. Does anyone have some advices or ideas how to do it?

Server side I can check if the cookie exits with $_SERVER['HTTP_COOKIE'] . The variable exits on my shared server and this works. But how to run wp_logout automaticly when cookie expires? Maybe by some javascript?

if(strpos($_SERVER['HTTP_COOKIE'], 'wordpress_logged_in') == true){
    echo "Login cookie exists!<br>";
    var_dump($_SERVER['HTTP_COOKIE']);
    //echo($startTime = microtime(true));
} else{
    echo "Login cookie not exists!";
    var_dump($_SERVER['HTTP_COOKIE']);
    }

Simply change session expire time, as mentioned , by adding in your theme's functions.php something like:

add_filter('auth_cookie_expiration', 'my_expiration_filter', 99, 3);
function my_expiration_filter($seconds, $user_id, $remember){

    //if "remember me" is checked;
    if ( $remember ) {
        //WP defaults to 2 weeks;
        $expiration = 14*24*60*60; //UPDATE HERE;
    } else {
        //WP defaults to 48 hrs/2 days;
        $expiration = 2*24*60*60; //UPDATE HERE;
    }

    //http://en.wikipedia.org/wiki/Year_2038_problem
    if ( PHP_INT_MAX - time() < $expiration ) {
        //Fix to a little bit earlier!
        $expiration =  PHP_INT_MAX - time() - 5;
    }

    return $expiration;
}

Surprisingly enough, WordPress does not use PHP sessions at all. It uses only cookies. And it uses a number of them with hashed names.

So don't mess with them directly, and do it the WordPress way with filters like above.

Or by calling WordPress APIs. Eg if you want to log out the current user, you can call wp_clear_auth_cookie : http://codex.wordpress.org/Function_Reference/wp_clear_auth_cookie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM