Blog | G5 Cyber Security

PHP Bot Detection

TL;DR

This guide shows simple ways to spot bots in your PHP applications. We’ll cover checking user agents, using CAPTCHAs, looking for unusual activity, and basic IP address blocking.

1. Check the User Agent

Bots often have identifiable user agent strings. Humans usually use web browsers like Chrome, Firefox or Safari. You can check this in PHP:

Important: Bots can *spoof* user agents. This isn’t foolproof.

2. Implement CAPTCHAs

CAPTCHAs (Completely Automated Public Turing test to tell Computers and Humans Apart) are a common way to verify humans. There are several PHP libraries available, such as reCAPTCHA from Google:

Example (basic concept – you’d need a full library implementation):

3. Monitor for Unusual Activity

Bots often behave differently than humans:

Example (checking request rate – simplified):

 10) {
  // Too many requests from this IP!
  echo "Rate Limit Exceeded";
}
?>

Note: This is a basic example. You’ll need to adjust the threshold (10 in this case) based on your application.

4. Basic IP Address Blocking

If you identify malicious IPs, you can block them:

Example (.htaccess):

<FilesMatch ".*">
Order Deny,Allow
Deny from 123.45.67.89
Allow from all
</FilesMatch>

Warning: Blocking IPs can accidentally block legitimate users. Be careful!

5. Session Management

Proper session management helps identify bots that don’t handle sessions correctly.

6. Honeypots

Add hidden form fields that only bots are likely to fill out. If these fields are populated, it’s a strong indicator of a bot.

<input type="text" name="honeypot" style="display:none;">

7. Cyber security Considerations

These methods provide basic protection. For robust cyber security, consider using:

Exit mobile version