/**
 * Only allow one session per user
 *
 * If the current user's session has been taken over by a newer
 * session then we will destroy their session automattically and
 * they will have to login again to continue.
 *
 * @action init
 */
function pcl_prevent_concurrent_logins()
{
    if (!pcl_user_has_concurrent_sessions()) {
        return;
    }
    $user_id = get_current_user_id();
    /**
     * Filter to allow certain users to have concurrent sessions when necessary
     *
     * @since 0.1.1
     *
     * @param bool $prevent
     * @param int  $user_id ID of the current user
     *
     * @return bool
     */
    if (false === (bool) apply_filters('pcl_prevent_concurrent_logins', true, $user_id)) {
        return;
    }
    $newest = max(wp_list_pluck(wp_get_all_sessions(), 'login'));
    $session = pcl_get_current_session();
    if ($session['login'] === $newest) {
        wp_destroy_other_sessions();
        /**
         * Fires after a user's non-current sessions are destroyed
         *
         * @since 0.3.0
         *
         * @param int $user_id ID of the affected user
         */
        do_action('pcl_destroy_other_sessions', $user_id);
    } else {
        wp_destroy_current_session();
        /**
         * Fires after a user's current session is destroyed
         *
         * @since 0.3.0
         *
         * @param int $user_id ID of the affected user
         */
        do_action('pcl_destroy_current_session', $user_id);
    }
}
 function settings_tools()
 {
     if (isset($_POST['action'])) {
         switch ($_POST['action']) {
             case 'force_destroy_sessions':
                 if (0 === get_current_user_id()) {
                     MainWP_Helper::write(array('error' => __('Cannot get user_id', 'mainwp-child')));
                 }
                 wp_destroy_all_sessions();
                 $sessions = wp_get_all_sessions();
                 if (empty($sessions)) {
                     MainWP_Helper::write(array('success' => 1));
                 } else {
                     MainWP_Helper::write(array('error' => __('Cannot destroy sessions', 'mainwp-child')));
                 }
                 break;
             default:
                 MainWP_Helper::write(array('error' => __('Invalid action', 'mainwp-child')));
         }
     } else {
         MainWP_Helper::write(array('error' => __('Missing action', 'mainwp-child')));
     }
 }
Exemple #3
0
				<h3 id="sessions"><?php 
            _e('会话安全', 'dmeng');
            ?>
 <small><?php 
            _e('登录状态', 'dmeng');
            ?>
</small></h3>
			</div>
	<div class="form-group">
		<label for="destroy-sessions" class="col-sm-3 control-label"><?php 
            _e('清理会话', 'dmeng');
            ?>
</label>
		<div class="col-sm-9">
			<?php 
            $count_sessions = count(wp_get_all_sessions());
            if ($count_sessions > 1) {
                ?>
				<button type="submit" class="btn btn-default"><?php 
                printf(__('共有 %1$s 个会话,点击登出其他会话', 'dmeng'), $count_sessions);
                ?>
</button>
			<?php 
            } else {
                ?>
				<button type="button" class="btn btn-default" disabled><?php 
                _e('无需清理,您只有在此处登入。', 'dmeng');
                ?>
</button>
			<?php 
            }