👋🏼Welcome to my WP-Host blog where I am excited to share my knowledge and expertise on WordPress hosting and website construction tutorials with you. Let’s connect and learn from each other! You can reach me at info@yrshare.com.
Keep sharing wordpress tutorials. Website security is one of the issues we must pay attention to. There are many website background login systems that allow you to enter passwords unlimited times, so in theory, as long as you keep trying, hackers may crack your website passwords, thus affecting your age.
Carrying out illegal operations will have serious consequences.
So what to do? Next【WP-Host/悦然wordpress建站】will share with you the method of limiting the number of user logins.
Method 1: Use the code
We can add the following code to the functions.php file of the current wordpress website building theme to achieve the effect of limiting the number of user logins:
/*-----------------------------------------------------------------------------------*/
/* Limit the number of user logins to protect your wordpress website
/*-----------------------------------------------------------------------------------*/
# 阻止登录的函数,直接 404
function block_login() {header("HTTP/1.1 404 Not Found");header("Status: 404 Not Found");exit;}
# 登录前判断登陆的失败次数
add_action("login_head",function () {
$login_ip = $_SERVER['REMOTE_ADDR'];
$login_ip_list = unserialize(get_option("LOGIN_IP_LIST"));
# 登录失败超过2次就进行阻止登陆
if($login_ip_list && ($login_ip_list[$login_ip] > 2)) {block_login();}});
# 登录失败的处理
add_action('login_errors', function ($info) {
$login_ip = $_SERVER['REMOTE_ADDR'];
$login_ip_list = get_option("LOGIN_IP_LIST");
if($login_ip_list) {$login_ip_list = unserialize($login_ip_list);} else {$login_ip_list = array();}
# 登录次数 + 1
$login_ip_list[$login_ip] += 1;
update_option('LOGIN_IP_LIST', serialize($login_ip_list));
# 提示登录失败的次数
return "$login_ip 登陆次数 " . $login_ip_list[$login_ip];});
# 证明已经登录成功了
add_action("admin_menu", function () {
$login_ip = $_SERVER['REMOTE_ADDR'];
$login_ip_list = unserialize(get_option("LOGIN_IP_LIST"));
$login_ip_list[$login_ip] = 0;
update_option('LOGIN_IP_LIST', serialize($login_ip_list));});
The above code can limit the number of logins according to the IP. The default is to limit the number of logins if it exceeds 2 times. You can also modify the value according to the actual situation. The Chinese notes inside can be modified or deleted.
Adding code to the functions.php file may be affected by updating the theme or updating the theme. 【WP-Host/悦然wordpress建站】recommends that you use the Code Snippets plugin instead of the functions.php file.
Code Snippets
https://wordpress.org/plugins/code-snippets/
Method 2: Use a plugin
In addition to usage and code, we can also use wordpress plugins to limit the number of user logins. The wordpress plugin recommended here is Limit Login Attempts Reloaded.
The Limit Login Attempts Reloaded plugin can be downloaded in the plugin center, or can be downloaded and installed through the link above.
After the plug-in is installed and enabled, you only need to make simple settings, and then save it to take effect.
Method 3: Use the Site Ground Security Plugin
If you are using a Site Ground hosting, you can directly use their wordpress security plug-in SiteGround Security, which is usually provided with the host. You only need to install wordpress in the background of the Site Ground hosting with one click, and it will be installed automatically. Then you go directly to configure and use it.
Summarize
The above is today’s wordpress tutorial. I hope everyone can pay attention to website security issues. In addition to limiting the number of user logins, we can further improve website security performance through firewalls and other methods.