-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.php
More file actions
48 lines (38 loc) · 1.44 KB
/
Copy pathbasic-usage.php
File metadata and controls
48 lines (38 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
require_once __DIR__ . '/../PHPBotDetect.php';
// Optional: point to a writable directory for logs and cache (rDNS, behavior).
// If omitted, the system's temporary directory will be used.
PHPBotDetect::setBaseDirectory(__DIR__ . '/../storage');
// Basic usage: detect a bot in the current request.
if (PHPBotDetect::detect()) {
http_response_code(403);
echo 'Access denied.';
exit;
}
// With logging of detected bots to a CSV file (disabled by default):
$isBot = PHPBotDetect::detect(logs: true);
// DNS verification and behavioral analysis disabled (e.g. under very high traffic):
$isBot = PHPBotDetect::detect(logs: true, checkDns: false, checkBehavior: false);
// Example form with a honeypot field:
?>
<form method="post">
<input type="text" name="email" placeholder="Your email">
<!-- Honeypot field: must remain empty and invisible to humans -->
<input type="text"
name="<?= PHPBotDetect::HONEYPOT_FIELD_NAME ?>"
value=""
autocomplete="off"
tabindex="-1"
style="position:absolute; left:-9999px;">
<button type="submit">Submit</button>
</form>
<?php
// Checking the honeypot when processing the form:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (PHPBotDetect::honeypotTriggered()) {
// The field was filled in — almost certainly a bot. Silently reject the request.
exit;
}
// ... further form processing
}