How to block Baidu with PHP?

Blocking Baidu (or any other user agent or IP address) with PHP can be done by checking the user agent or IP address of the incoming request and denying access if it matches the one you want to block. Here's an example of how to block Baidu using the user agent:

if (strpos($_SERVER['HTTP_USER_AGENT'], 'Baidu') !== false) {
    header("HTTP/1.0 403 Forbidden");
    exit;
}

This code checks the user agent of the incoming request, and if it contains the string "Baidu", it sends a 403 Forbidden response to the client and exits the script.

Alternatively, you can check the IP address of the incoming request and block it if it matches the one you want to block. Here's an example of how to block an IP address:

if ($_SERVER['REMOTE_ADDR'] == '123.456.789.0') {
    header("HTTP/1.0 403 Forbidden");
    exit;
}

This code checks the IP address of the incoming request, and if it matches the one you want to block, it sends a 403 Forbidden response to the client and exits the script.

It's important to note that this method of blocking IP addresses is not foolproof, as IP addresses can be easily spoofed, and it's not recommended to rely on IP addresses alone to secure your application. Additionally, blocking IP addresses or user agents can be bypassed by using a proxy server or VPN.

It's also worth noting that blocking Baidu or any other search engine could have a negative impact on your website's visibility and search engine optimization.

kabeer

Added  Jan 26, 01:45 am

Article tags

×
SignUp with Email
X