WordPress 禁止同一账户多人登录代码

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A


session_id = session_id();

add_action( 'init', array( $this, 'wpsl_init' ) );
add_action( 'wp_login', array( $this, 'wpsl_login' ), 10, 2 );
add_filter('heartbeat_received', array( $this, 'wpsl_heartbeat_received' ), 10, 2);
add_filter('heartbeat_nopriv_received', array( $this, 'wpsl_heartbeat_received' ), 10, 2);
add_filter( 'login_message', array( $this, 'wpsl_loggedout_msg' ), 10 );
}

function wpsl_init() {
if( ! is_user_logged_in() )
return;
//enqueue the Heartbeat API
wp_enqueue_script('heartbeat');
wp_enqueue_script('jquery');

//load our Javascript in the footer
add_action("wp_footer", array( $this, 'wpsl_scripts' ) );
$user_sess_id = get_user_meta( get_current_user_id(), '_wpsl_hash', true );

if( $user_sess_id != $this->session_id ) {
wp_logout();
wp_redirect( site_url( 'wp-login.php?wpsl=loggedout' ) );
exit;
}
}
function wpsl_login( $user_login, $user ) {
update_user_meta( $user->ID, '_wpsl_hash', $this->session_id );
return;
}
function wpsl_loggedout_msg() {
if ( isset($_GET['wpsl']) && $_GET['wpsl'] == 'loggedout' ) {
$msg = __( "该账号已在别处设备登录,请及时重置密码!" ) ;
$message = '

'.$msg.'

';
return $message;
}
}
function wpsl_heartbeat_received($response, $data) {
$user_sess_id = get_user_meta( get_current_user_id(), '_wpsl_hash', true );
if( $data['user_hash'] && $data['user_hash'] != $user_sess_id ){
$response['wpsl_response'] = 1;
wp_logout();
}
else
$response['wpsl_response'] = 0;

return $response;
}

function wpsl_scripts() { ?>

Leave a Comment