有没有办法让Wordpress无效共享用户名(基于IP地址或在不同位置同时登录)?如果有一个插件只是为了跟踪IP地址和登录时间,它也应该没问题。感谢
答案 0 :(得分:2)
你可以将它添加到你的functions.php或插件文件中。
//set the most current user to have a cookie matching a unique value
add_action("set_logged_in_cookie", "one_cookie", 10, 5);
function one_cookie($logged_in_cookie, $expire, $expiration, $user_id, $logged_in) {
$secure = apply_filters('secure_logged_in_cookie', false, $user_id, is_ssl());
$cookie = uniqid();
update_user_meta($user_id, "one_cookie", $cookie);
setcookie("one_cookie", $cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure, true);
return;
}
//check requests from users to ensure they have this cookie
add_action("init", "check_one_cookie", 1);
function check_one_cookie() {
$user = wp_get_current_user();
if ($user->ID == 0) { return; }
$storedcookie = get_user_meta($user->ID, 'one_cookie');
print_r(array('$storedcookie'=>$storedcookie));
if (!empty($storedcookie) && $_COOKIE['one_cookie'] != $storedcookie) {
/*if the user doesn't have the same cookie as we have stored, log them out.*/
wp_logout();
//auth_redirect() may have a more desired effect
}
}
//unset a users cookie
add_action('wp-logout', 'one_cookie_logout');
function one_cookie_logout() {
setcookie("one_cookie", "", 1);
}
但这只能在一个方向上起作用。每次处理新登录时,它都会锁定旧登录。如果您想要反转,您可能需要编写更多代码,以便用户可以打破锁定等。
你也可以通过替换'wp-includes / pluggable.php'中的相应函数来实现这一目标。
我已经测试了上面的代码以使用WordPress 3.1。
答案 1 :(得分:1)
在3.6.1中测试并且运行良好,但函数“check_one_cookie”必须像这样更新:
add_action("init", "check_one_cookie", 1);
function check_one_cookie() {
$user = wp_get_current_user();
if ($user->ID == 0) { return; }
$storedcookie = get_user_meta($user->ID, 'one_cookie');
// print_r(array('$storedcookie'=>$storedcookie));
if (!empty($storedcookie) && $_COOKIE['one_cookie'] != $storedcookie[0]) {
/*if the user doesn't have the same cookie as we have stored, log them out.*/
wp_logout();
//auth_redirect() may have a more desired effect
}
}
应该通过 $ storedcookie [0] 来关联$ storedcookie (并且不要忘记用您自己的数据替换COOKIEPATH,COOKIE_DOMAIN)
答案 2 :(得分:0)
我没有用过这个,但IP Logger WordPress plugin看起来很有希望。它有很多日志和管理功能。