Wordpress:立即销毁会话并重定向用户

时间:2017-05-22 20:01:26

标签: wordpress session

我有一个脚本,当该用户登录到wordpress时,通过销毁用户的所有其他活动会话,只允许一个并发的wordpress会话:

add_filter('authenticate', 'wpse_12282015_single_login_authenticate', 0, 3);

function wpse_12282015_single_login_authenticate($user, $username, $password) {

    $user =  get_user_by('login', $username);

    if( isset($user->ID) ){

        if(isset($user->roles) && is_array($user->roles)) {

            //check for admins
            if(in_array('administrator', $user->roles)) {

                // admin can log in more than once
                return $user;
            }
        }

        // get all sessions for user
        $sessions = WP_Session_Tokens::get_instance($user->ID);

        // destroy everything since we'll be logging in shortly
        $sessions->destroy_all();
    }

    return $user;
}

现在,我想添加一个功能,可以自动将已销毁的会话(如果已打开)重定向到另一个页面。

例如,如果用户通过Chrome浏览器登录并在Firefox中同时登录,则Chrome中的页面应检测已销毁的会话(例如,每隔10秒通过ajax循环)并自动重定向到一页。

我怎么能实现这个目标?

0 个答案:

没有答案