Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Get user IP in WordPress
function get_the_user_ip() {
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return apply_filters( 'wpb_get_ip', $ip );
}
function search_visitor_ip_block($query) {
// Visitor search limit
$visitorIPLimit = 20;
$visitorIPCount = get_option( 'visitor_ip_count'.get_the_user_ip() );
//Daha Zamanı var mı soralım
$is_block = get_transient( 'visitor_ip_block'.get_the_user_ip() );
$will_deleted = get_option('visitor_ip_block'.get_the_user_ip().'-deleteafter30min');
//Sonrasında blok kontrolü yapalım
if($is_block == 'blocked') :
wp_die('Sakin ol Şampiyon? Daha blok süren bitmemiş :)');
else :
//Daha varmış normal prosedür devam etsin
if ( $query->is_search ) :
if(($visitorIPCount >= $visitorIPLimit) && $will_deleted == 'yes') :
delete_option('visitor_ip_count'.get_the_user_ip());
delete_option('visitor_ip_block'.get_the_user_ip().'-deleteafter30min');
//Limitimizden aşağıda mı kontrol edip ekleyelim
elseif( $visitorIPCount < $visitorIPLimit ) :
$visitorIPCount += 1 ;
update_option( 'visitor_ip_count'.get_the_user_ip(), $visitorIPCount );
return $query;
else :
//30 Dakika engel için geçici opsiyon atayalım
set_transient('visitor_ip_block'.get_the_user_ip(),'blocked',30);
update_option('visitor_ip_block'.get_the_user_ip().'-deleteafter30min','yes');
//Sorguyu öldürelim
wp_die('Sakin ol Şampiyon?');
endif;
endif;
endif;
}
//hook filters to search
add_filter('pre_get_posts','search_visitor_ip_block');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.