How WhoKnew Views™ identifies bots using HTTP headers and pattern matching
When any visitor—human or bot—requests a page from your store, their browser or crawler sends an HTTP header called User-Agent. This header is the foundation of WhoKnew Views™ bot detection. Understanding how it works helps you write more effective filter patterns.
🌐 What Is a User-Agent String?
A User-Agent is a text string that identifies the software making a web request. It typically includes the browser name, version, operating system, and sometimes rendering engine:
👤 Example: Real Human Visitor (Chrome on Windows)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36🤖 Example: Bot Visitor (Googlebot)
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)🤖 Example: AI Crawler (GPTBot)
Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.2; +https://openai.com/gptbot)ℹ️ Key Insight: Bots usually identify themselves honestly in their User-Agent because website owners can use robots.txt to block them. Legitimate crawlers follow these rules and announce who they are.
🔄 The Detection Flow
Here’s exactly what happens when someone views a product page:
1
📥 HTTP Request Received
WordPress receives the page request. The browser/bot’s User-Agent is in the HTTP headers.
$_SERVER[‘HTTP_USER_AGENT’]
2
🛡️ WhoKnew Views™ Intercepts
Before counting the view, WhoKnew Views™ calls wkv_is_bot_traffic() to check if this is a bot.
if (wkv_is_bot_traffic()) return; // Block
3
🔍 User-Agent Extraction
The User-Agent string is read and sanitized using WordPress functions.
$user_agent = wkv_get_user_agent();
4
📋 Pattern Loop
Each pattern in your filter list is tested against the User-Agent using stripos().
foreach ($bot_list as $entry) { … }
5
✅ Decision Made
If ANY pattern matches → BLOCK (exit, no view counted). If NO patterns match → ALLOW (continue counting).
💻 The Core Functions
WhoKnew Views™ uses three PHP functions for User-Agent detection:
📥 Function 1: Get User-Agent
function wkv_get_user_agent() {
if (isset($_SERVER['HTTP_USER_AGENT']) && !empty($_SERVER['HTTP_USER_AGENT'])) {
return sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT']));
}
return false; // No User-Agent header present
}Reads the raw HTTP header, sanitizes it for security, and returns it (or false if missing).
🔍 Function 2: Pattern Matching
function wkv_user_agent_matches_pattern($user_agent, $pattern) {
// Case-insensitive substring search
return stripos($user_agent, $pattern) !== false;
}Uses PHP’s stripos() — finds $pattern anywhere inside $user_agent, ignoring case.
🛡️ Function 3: Bot Check (Main Function)
function wkv_is_bot_traffic() {
$user_agent = wkv_get_user_agent();
if (!$user_agent) return false; // No UA = allow tracking
$bot_list = get_option('whoknew_views_bot_filter', array());
if (empty($bot_list)) return false; // No filters = allow
foreach ($bot_list as $entry) {
if (wkv_user_agent_matches_pattern($user_agent, $entry['pattern'])) {
wkv_log_activity(0, 'bot_blocked', "Pattern matched");
return true; // BOT DETECTED - block tracking
}
}
return false; // No match - allow tracking
}🎯 How Pattern Matching Works
The stripos() function is the heart of bot detection. Here’s exactly how it evaluates patterns:
| User-Agent String | Pattern | Result | Why |
|---|---|---|---|
| …compatible; Googlebot/2.1… | Googlebot | ✅ MATCH | Exact substring found |
| …compatible; GOOGLEBOT/2.1… | googlebot | ✅ MATCH | Case-insensitive |
| …AppleWebKit… GPTBot/1.2… | gpt | ✅ MATCH | Partial match works |
| MyBotCrawler/1.0 | bot | ✅ MATCH | Found in middle of word |
| Mozilla/5.0 (Windows NT 10.0…) | Googlebot | ❌ NO MATCH | Pattern not present |
| …Chrome/120.0.0.0 Safari/537.36 | Chrome | ⚠️ MATCH | Danger! Blocks real browsers |
⚠️ Warning: Substring matching cuts both ways. A pattern like bot will match robot, Googlebot, and MyBotCrawler—but it won’t match b0t (number zero) or b o t (with spaces).
🔧 Visual: The Matching Algorithm
How stripos() Scans the User-Agent
User-Agent:
Mozilla/5.0 (compatible; SemrushBot/7~bl; +http://semrush.com/bot.html)
Pattern: semrush
✅ Found at position 26 → BOT DETECTED
Position
0-25
No match
Position
26
S found!
Position
27-32
e-m-r-u-s-h
Result
✅
MATCH!
Action
🚫
BLOCK
🔄 Integration with View Tracking
Bot detection happens before any view counting logic. Here’s where it sits in the flow:
1️⃣
IP Whitelist Check
Is IP excluded?
2️⃣
Bot Filter Check
Is User-Agent a bot?
3️⃣
Cookie/Fingerprint
Check refresh cooldown
4️⃣
Count View
Increment real + padded
💡 Critical Placement
// From view-counter-logic.php:
if (function_exists('wkv_is_ip_whitelisted') && wkv_is_ip_whitelisted()) {
return; // Exit - IP is excluded
}
if (function_exists('wkv_is_bot_traffic') && wkv_is_bot_traffic()) {
return; // Exit - it's a bot
}
// Only if BOTH checks pass does view counting continue...🔐 The Visitor Fingerprint System
Beyond bot filtering, User-Agent data is also used to create visitor fingerprints for deduplication:
🔑 Fingerprint Formula
$remote_addr = $_SERVER['REMOTE_ADDR']; // e.g., "192.168.1.100"
$user_agent = $_SERVER['HTTP_USER_AGENT']; // e.g., "Mozilla/5.0..."
$visitor_fingerprint = md5($remote_addr . '|' . $user_agent);
// Result: "a3f5b2c8d4e6f7a8b9c0d1e2f3a4b5c6" (32-char hash)This fingerprint is used for:
- Cookie naming:
whoknew_view_{product_id}_{fingerprint} - Server-side cooldown tracking (incognito mode fallback)
- First-time vs. returning visitor detection
📝 Debug Logging (When WP_DEBUG Enabled)
With WordPress debug mode active, WhoKnew Views™ logs detailed bot filter activity:
📋 Example Debug Log Output
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🤖 WKV BOT FILTER CHECK
User-Agent: Mozilla/5.0 (compatible; SemrushBot/7~bl; +http://semrush.com/bot.html)
Bot List Count: 15
Checking bot pattern #0: GPTBot
Testing pattern: "GPTBot" against User-Agent
Checking bot pattern #1: Semrush
Testing pattern: "Semrush" against User-Agent
✅ BOT DETECTED - BLOCKING tracking
Matched Pattern: "Semrush"
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ℹ️ Enable Debug Mode: Add these lines to your wp-config.php:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);Then check /wp-content/debug.log for output.
📊 System Info Activity Logging
Blocked bots are also logged to the System Info panel for Pro users:
🚫
bot_blocked
Bot detected: pattern ‘Semrush’ matched – view blocked
2 min ago
View blocked events in WhoKnew Views → Admin → System Info
⚠️ Edge Cases & Limitations
❌ Missing User-Agent
If the header is missing (rare), the visitor is allowed through. Blocking by default would risk false positives on legitimate traffic.
⚠️ Spoofed User-Agents
Malicious bots can fake any User-Agent. This system blocks honest bots—not sophisticated scrapers pretending to be Chrome.
✅ Empty Filter List
If no patterns are configured, all traffic is allowed. The function exits early without wasting resources.
🔒 No Regex Support
Patterns are plain text only. No wildcards, no regex. This is intentional—keeps it simple and fast.
💡 Key Takeaways
- User-Agent is an HTTP header sent with every request identifying the browser/bot
- WhoKnew Views™ reads it via
$_SERVER['HTTP_USER_AGENT'] - Matching uses
stripos()— case-insensitive substring search - Any pattern match → block the view (exit before counting)
- No patterns match → allow the view (continue to counting logic)
- Blocked bots are logged to System Info and debug.log
- User-Agent + IP = fingerprint for deduplication