尽管这种方法可能存在缺陷,因为它可以使用代理进行旁路,但这里有一种过于简单(尚未测试)的方法you would need to improve upon 但是会给你实现你想要的目标打下基础。
在我看来,这个过程:
筛选上的用户注册pre_user_login
或pre_user_nicename
钩住数据库,查看IP是否存在于时间限制的黑名单中。如果IP存在于范围内,则使用自定义错误消息拒绝注册。如果IP不存在于范围内,则将IP添加到时间限制的黑名单中。每次注册尝试时,请清洗并重复此操作。示例:
function filter_user_registration_ip($user_nicename) {
$ip = $_SERVER[\'REMOTE_ADDR\']; //get current IP address
$time = time(); //get current timestamp
$blacklist = get_option(\'user_ip_blacklist\') ?: array(); //get IP blacklist
/*
* If IP is an array key found on the resulting $blacklist array
* run a differential of the
*
*/
if ( array_key_exists($ip, $blacklist) ) {
/*
* Find the difference between the current timestamp and the timestamp at which
* the IP was stored in the database converted into hours.
*/
$diff_in_hours = ($time - $blacklist[$ip]) / 60 / 60;
if ( $diff_in_hours < 24 ) {
/*
* If the difference is less than 24 hours, block the registration attempt
* and do not reset or overwrite the timestamp already stored against the
* current IP address.
*/
wp_die(\'Your IP is temporarily blocked from registering an account\');
}
}
/*
* If the IP address does not exist, add it to the array of blacklisted IPs with
* the current timestamp (now).
*
* Or if the IP address exists but is greater than 24 hours in difference between
* the original stored timestamp and the current timestamp, add it to the array
* of blacklisted IPs.
*/
$blacklist[$ip] = $time;
update_option(\'user_ip_blacklist\', $blacklist);
return $user_nicename;
}
add_filter(\'pre_user_nicename\', \'filter_user_registration_ip\', 10, 1);
备注:
上述代码为untested 和may contain errors.检索当前用户IP的方法并非万无一失