PHP Firewall is a comprehensive, free PHP security script that protects all websites written in PHP.
Requirements:
- PHP 7.4+ or PHP 8.x
- No database required (flatfile system)
- No external dependencies
Features:
- Very small, simple, and easy to install
- Built-in logging system (JSON and TXT formats)
- Email alert system
- No .htaccess file required for better performance
- Object-Oriented Architecture with modern PHP standards
- XSS Protection - Cross-Site Scripting attack prevention
- SQL Injection Protection - UNION, SELECT, INSERT, UPDATE, DELETE injection blocking
- Command Injection Protection - Shell command execution prevention
- Path Traversal Protection - Directory traversal attack blocking
- File Inclusion Protection - LFI/RFI attack prevention
- Header Injection Protection - HTTP header manipulation blocking
- Rate Limiting - Configurable request limits per minute
- Brute Force Protection - Failed login attempt tracking and temporary lockout
- CSRF Protection - Cross-Site Request Forgery token validation
- Honeypot Fields - Bot detection using hidden form fields
- JSON/XML Bomb Protection - Malicious payload structure detection
- File Upload Security - Extension, MIME type, and PHP code validation
- Bad Bots Protection - SQLMap, Nikto, Nmap, and other scanner detection
- Bad Request Methods Protection - Dangerous HTTP method blocking
- DOS Protection - Basic denial-of-service mitigation
- Proxy Detection - Proxy header identification
- User-Agent Validation - Empty and suspicious user-agent blocking
- URL Query Protection - Malicious query string filtering
- Cookies Sanitize - Cookie value validation
- POST Vars Sanitize - POST data filtering
- GET Vars Sanitize - GET parameter filtering
- Request Body Validation - JSON and form data validation
- IP Range Reserved Denied - Private/reserved IP blocking
- IP Range Spam Denied - Known spam IP range blocking
- IP Whitelist Support - Trusted IP exemption
- IP Blacklist Support - Permanent IP blocking
- CIDR Notation Support - Flexible IP range definitions
- Security Headers - CSP, X-Frame-Options, X-Content-Type-Options
- Session Security - Secure session configuration
- Error Handling - Production-safe error management
Add this code to the header section of your site (before any output):
<?php
// Define constants before including firewall
define('PHP_FIREWALL_REQUEST_URI', filter_var($_SERVER['REQUEST_URI'] ?? '', FILTER_SANITIZE_URL));
define('PHP_FIREWALL_ACTIVATION', true);
// Include the firewall
include_once('firewall/firewall.php');<?php
// Firewall Configuration
define('PHP_FIREWALL_REQUEST_URI', filter_var($_SERVER['REQUEST_URI'] ?? '', FILTER_SANITIZE_URL));
define('PHP_FIREWALL_ACTIVATION', true);
// Optional: Custom configuration
define('PHP_FIREWALL_LOG_DIR', __DIR__ . '/logs');
define('PHP_FIREWALL_ADMIN_EMAIL', 'admin@example.com');
define('PHP_FIREWALL_RATE_LIMIT', 60); // Requests per minute
define('PHP_FIREWALL_BRUTE_FORCE_LIMIT', 5); // Failed attempts before lockout
define('PHP_FIREWALL_LOCKOUT_TIME', 900); // Lockout duration in seconds
include_once('firewall/firewall.php');| Constant | Default | Description |
|---|---|---|
PHP_FIREWALL_ACTIVATION |
true |
Enable/disable firewall |
PHP_FIREWALL_LOG_DIR |
__DIR__ . '/logs' |
Log directory path |
PHP_FIREWALL_ADMIN_EMAIL |
'' |
Admin email for alerts |
PHP_FIREWALL_RATE_LIMIT |
60 |
Max requests per minute |
PHP_FIREWALL_BRUTE_FORCE_LIMIT |
5 |
Max failed login attempts |
PHP_FIREWALL_LOCKOUT_TIME |
900 |
Lockout duration (seconds) |
PHP_FIREWALL_MAX_UPLOAD_SIZE |
10485760 |
Max file upload size (10MB) |
Edit the $whitelist_ips array in firewall.php:
private array $whitelist_ips = [
'127.0.0.1',
'::1',
'192.168.1.0/24', // CIDR notation supported
];Edit the $blacklist_ips array in firewall.php:
private array $blacklist_ips = [
'10.0.0.5',
'203.0.113.0/24',
];<?php
// In your form
?>
<form method="POST" action="/submit">
<?php echo phpf_csrf_field(); ?>
<?php echo phpf_honeypot_field(); ?>
<input type="text" name="username">
<input type="password" name="password">
<button type="submit">Login</button>
</form><?php
// In your login handler
if (!$login_successful) {
phpf_record_failed_login($username);
}<?php
// The firewall automatically validates uploads, but you can also use:
$firewall = PHPFirewall::getInstance();
if (isset($_FILES['upload'])) {
// Firewall has already validated the file
// Proceed with your upload logic
move_uploaded_file($_FILES['upload']['tmp_name'], $destination);
}<?php
$firewall = PHPFirewall::getInstance();
// Check if IP is whitelisted
if ($firewall->isWhitelisted($_SERVER['REMOTE_ADDR'])) {
// Skip additional checks
}The firewall includes a comprehensive bot blocking system with 200+ known bad bots.
<?php
// Include botlist after firewall
include_once('firewall/firewall.php');
include_once('firewall/botlist.php');
// Check and block bad bots
if (check_bot_user_agent()) {
// Bot detected - already logged, optionally redirect
header('HTTP/1.1 403 Forbidden');
exit('Access Denied');
}The botlist includes these categories:
| Category | Examples |
|---|---|
| Vulnerability Scanners | sqlmap, nikto, nuclei, wpscan, dirsearch, gobuster |
| AI/LLM Scrapers | GPTBot, ClaudeBot, Anthropic-AI, ByteSpider, Perplexity |
| Modern Scrapers | Scrapy, Puppeteer, Playwright, Selenium, PhantomJS |
| SEO Bots | AhrefsBot, SemrushBot, MJ12bot, DotBot, SimilarWeb |
| Spam Bots | XRumer, ScrapeBox, GSA Search Engine Ranker |
| Proxy Services | Brightdata, Oxylabs, Smartproxy |
| Credential Tools | Hydra, Medusa, CrackMapExec |
Allow legitimate bots (Google, Bing, etc.):
<?php
// Define whitelist before including botlist
$bot_whitelist = [
'Googlebot',
'Bingbot',
'YandexBot',
'DuckDuckBot',
'Slurp', // Yahoo
'facebot', // Facebook
'Twitterbot',
'LinkedInBot',
];
include_once('firewall/botlist.php');
// Check with whitelist support
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$is_bad_bot = false;
foreach ($bot_whitelist as $good_bot) {
if (stripos($user_agent, $good_bot) !== false) {
$is_bad_bot = false;
break;
}
}
if (!$is_bad_bot && check_bot_user_agent()) {
header('HTTP/1.1 403 Forbidden');
exit('Access Denied');
}Add your own bot patterns:
<?php
// Add custom patterns to the botlist array
$custom_bad_bots = [
'MyCustomBot',
'AnotherBadBot',
];
// Merge with existing list
$php_firewall_bad_bots = array_merge($php_firewall_bad_bots, $custom_bad_bots);Get information about blocked bots:
<?php
$stats = get_bot_list_stats();
echo "Total bot patterns: " . $stats['total_patterns'];
echo "Categories: " . implode(', ', $stats['categories']);
echo "Last updated: " . $stats['last_updated'];For Apache servers, you can also block bots via .htaccess:
# Block bad bots via .htaccess
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (sqlmap|nikto|nuclei|wpscan) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} (GPTBot|ClaudeBot|Anthropic) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} (AhrefsBot|SemrushBot|MJ12bot) [NC]
RewriteRule .* - [F,L]For Nginx servers:
# Block bad bots via nginx
if ($http_user_agent ~* (sqlmap|nikto|nuclei|wpscan|GPTBot|ClaudeBot|AhrefsBot)) {
return 403;
}The firewall creates logs in the configured log directory:
Human-readable log format:
[2024-01-15 10:30:45] BLOCKED | IP: 192.168.1.100 | Attack: SQL Injection | URI: /page?id=1' OR '1'='1
Machine-readable JSON format:
{
"timestamp": "2024-01-15T10:30:45+00:00",
"ip": "192.168.1.100",
"attack_type": "SQL Injection",
"uri": "/page?id=1' OR '1'='1",
"user_agent": "Mozilla/5.0...",
"blocked": true
}The firewall automatically sets these security headers:
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Referrer-Policy: strict-origin-when-cross-origin
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'
Permissions-Policy: geolocation=(), microphone=(), camera=()
If legitimate requests are being blocked:
- Check the log files to identify the trigger
- Add the IP to whitelist if trusted
- Adjust the pattern matching in the respective validator class
For high-traffic sites:
- Consider using Redis/Memcached for rate limiting instead of file-based
- Adjust
PHP_FIREWALL_RATE_LIMITbased on your traffic patterns - Enable PHP OPcache for better performance
This version is fully compatible with:
- PHP 7.4
- PHP 8.0
- PHP 8.1
- PHP 8.2
- PHP 8.3
New Features:
- Object-Oriented Architecture
- Rate Limiting
- Brute Force Protection
- CSRF Token Protection
- Honeypot Fields
- File Upload Validation
- JSON/XML Bomb Protection
- Command Injection Protection
- Path Traversal Protection
- Header Injection Protection
- Proxy Detection
- CIDR IP Range Support
- JSON Log Format
- Security Headers
Improvements:
- Type declarations (PHP 7.4+)
- Null coalescing operators
- Modern array syntax
- Singleton pattern
- Better error handling
- Improved performance
Removed:
register_globalshandling (removed in PHP 5.4)- Deprecated
eregfunctions - Legacy PHP 5.x support
This project is free to use for personal and commercial projects.
Original concept: https://seditio.com.tr
Updated for PHP 7.x/8.x with advanced security features.