SQL Injection Pattern (Basic Detection) Regex for PHP
/(?:;|--|#|/\*|\*/|\bUNION\b|\bSELECT\b|\bINSERT\b|\bUPDATE\b|\bDELETE\b|\bDROP\b|\bCREATE\b|\bALTER\b|\bEXEC\b|\bEXECUTE\b|\bxp_|\bsp_|'(?:--|OR|AND)\s+|OR\s+1\s*=\s*1|AND\s+1\s*=\s*1|'\s*OR\s*'|"\s*OR\s*"|CHAR\s*\(|CONCAT\s*\(|SLEEP\s*\(|WAITFOR\s+DELAY)/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching sql injection pattern (basic detection), ported and verified for PHP. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// SQL Injection Pattern (Basic Detection)
// ReDoS-safe | RegexVault — Security > Injection Patterns
define('SQL_INJECTION_PATTERN_BASIC_DETECTION_PATTERN', '/(?:;|--|#|\/\*|\*\/|\bUNION\b|\bSELECT\b|\bINSERT\b|\bUPDATE\b|\bDELETE\b|\bDROP\b|\bCREATE\b|\bALTER\b|\bEXEC\b|\bEXECUTE\b|\bxp_|\bsp_|\'(?:--|OR|AND)\s+|OR\s+1\s*=\s*1|AND\s+1\s*=\s*1|\'\s*OR\s*\'|"\s*OR\s*"|CHAR\s*\(|CONCAT\s*\(|SLEEP\s*\(|WAITFOR\s+DELAY)/');
function validate_sql_injection_pattern_basic_detection(string $input): bool {
return (bool) preg_match(SQL_INJECTION_PATTERN_BASIC_DETECTION_PATTERN, $input);
}
// Example
var_dump(validate_sql_injection_pattern_basic_detection("' OR 1=1--")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
' OR 1=1-- | hello world |
1; DROP TABLE users;-- | John's Coffee Shop |
UNION SELECT password FROM users | — |
' OR '1'='1 | — |
When to use this pattern
This pattern is drawn from the Security > Injection Patterns category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Regex-based SQL injection detection is insufficient as a primary defense — it cannot keep up with encoding variations (Unicode, URL encoding, hex encoding). The only reliable defense is parameterized queries (prepared statements) with an ORM.
Technical Notes
Detection pattern for WAF rules and input validation logging. HIGH false positive rate — legitimate inputs can match (e.g., user names with apostrophes, technical documentation). Use for alerting/logging rather than hard blocking. Always use parameterized queries as the real defense.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern