国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

WordPress教程:禁止多個人同時登錄一個用戶賬號

2024-04-25 20:43:35
字體:
來源:轉載
供稿:網友

關于 WordPress 禁止多個人同時登錄一個用戶賬號,倡萌之前就推薦過 Prevent Concurrent Logins ,今天推薦的 Wp Single Login 也可以實現一樣的功能,不過 Wp Single Login 是通過 WP 3.6 新增的 Heartbeat API 來實現的。

后臺插件安裝界面搜索 Wp Single Login 即可在線安裝,或者在這里下載 Wp Single Login ,直接安裝啟用即可,不需要設置。

當然,如果你不想用插件,或者想自定義某些代碼,以下就是該插件的源代碼:

<?php

/*

Plugin name: WP Single Login

Plugin URI: http://magnigenie.com/wp-single-login/

Description: This plugin will automatically logout the already logged in user when a user with the same login details tries to login from different browser or different computer. This plugin needs zero configuration to run. Just install it if you want single login functionality on your site.

Version: 1.0

Author: Nirmal Ram

Author URI: http://magnigenie.com/about-me/

License: GPLv2 or later

License URI: http://www.gnu.org/licenses/gpl-2.0.html

*/

if( !class_exists( 'wp_single_login' ) ) {

class wp_single_login {

private $session_id;

function __construct() {

if ( ! session_id() )

session_start();

$this->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 = __( "Your session has been terminated as you are logged in from another browser." ) ;

$message = '<p class="message">'.$msg.'</p><br />';

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() { ?>

<script>

jQuery(document).ready(function() {

wp.heartbeat.interval( 'fast' );

//hook into heartbeat-send: and send the current session id to the server

jQuery(document).on('heartbeat-send', function(e, data) {

data['user_hash'] = '<?php echo $this->session_id; ?>'; //need some data to kick off AJAX call

});

//hook into heartbeat-tick: client looks for a 'server' var in the data array and logs it to console

jQuery(document).on( 'heartbeat-tick', function( e, data ) {

if( data['wpsl_response'] ){

alert( '<?php _e('Your session has been terminated as you are logged in from another browser.'); ?>' );

window.location.href='<?php echo site_url( 'wp-login.php?wpsl=loggedout' ); ?> ';

}

});

});

</script>

<?php

}

}

new wp_single_login();

}

以上就是本文章的內容,希望對大家有所幫助

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁化县| 历史| 政和县| 南投市| 桦川县| 玉田县| 洛川县| 晋中市| 遂溪县| 三江| 临湘市| 呼和浩特市| 仙居县| 西平县| 苍山县| 大连市| 剑阁县| 沂南县| 皋兰县| 温州市| 班戈县| 城步| 九江市| 全椒县| 东安县| 福海县| 蒙城县| 垫江县| 九龙县| 兴宁市| 彝良县| 乌兰县| 铜梁县| 巴彦县| 阳泉市| 延津县| 乡城县| 盐源县| 湘潭市| 周口市| 宜都市|