亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Why addslashes() is not enough
Use Prepared Statements (Parameterized Queries)
Example using PDO (PHP):
Example using MySQLi:
When You Can’t Use Prepared Statements (Rare)
1. Escaping Strings: Use Database-Specific Functions
2. Escaping Identifiers (Table/Column Names)
3. Integers and Other Types
Summary: Defense-in-Depth with Context
Home Backend Development PHP Tutorial Beyond `addslashes()`: Contextual Escaping for Robust SQL Injection Defense

Beyond `addslashes()`: Contextual Escaping for Robust SQL Injection Defense

Jul 26, 2025 am 02:55 AM
PHP Escape Characters

SQL注入防護不能依賴addslashes(),因其不處理多字節(jié)編碼且僅轉(zhuǎn)義有限字符,易被繞過;應使用預處理語句(如PDO或MySQLi的參數(shù)化查詢)將數(shù)據(jù)與SQL邏輯分離,確保輸入不被解析為代碼;若無法使用預處理,需根據(jù)上下文采用數(shù)據(jù)庫特定的轉(zhuǎn)義函數(shù)(如real_escape_string并設(shè)置正確字符集)、標識符白名單或引號包裹、整型輸入強制類型轉(zhuǎn)換等方法,實現(xiàn)分層防御。

"Beyond

When it comes to defending against SQL injection, relying solely on addslashes() is a dangerous shortcut. While it might seem to work in some cases, it’s fundamentally flawed and insufficient for robust security. True protection requires contextual escaping—applying the right escaping method based on the specific context in which user data enters the SQL query.

"Beyond

Let’s break down why addslashes() fails and what you should use instead.


Why addslashes() is not enough

addslashes() adds backslashes before quotes and a few other characters (', ", \, and NULL). But here’s the problem:

"Beyond
  • It doesn’t account for different character encodings (like multi-byte encodings such as GBK), where an escaped character might be interpreted as part of a two-byte sequence, leading to exploitable vulnerabilities.
  • It only escapes a limited set of characters, and its behavior isn’t tied to the actual SQL grammar or database driver.
  • It assumes single-byte encodings and may fail under edge cases, making it unreliable.

Example: In a GBK encoding context, '\' followed by %bf%27 can be interpreted as a single character, allowing an attacker to bypass addslashes() and inject ' into the query.

Bottom line: Never use addslashes() for SQL escaping.

"Beyond

Use Prepared Statements (Parameterized Queries)

The gold standard for preventing SQL injection is prepared statements with parameterized queries. This approach separates SQL logic from data, so user input is never interpreted as part of the SQL command.

Example using PDO (PHP):

$pdo = new PDO($dsn, $user, $pass);

$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$userEmail]);

$user = $stmt->fetch();

Example using MySQLi:

$mysqli = new mysqli("localhost", "user", "pass", "db");

$stmt = $mysqli->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $userEmail);
$stmt->execute();
$result = $stmt->get_result();

? Why it works:

  • The SQL structure is fixed at prepare time.
  • User data is sent separately and treated strictly as data.
  • No amount of clever input can alter the query logic.

This is context-aware by design—you don’t need to escape manually because the database driver handles it safely.


When You Can’t Use Prepared Statements (Rare)

In rare cases—like dynamic table names or complex full-text search—you can’t use placeholders. In these situations, contextual escaping becomes essential.

1. Escaping Strings: Use Database-Specific Functions

Instead of addslashes(), use the escaping function provided by your database driver:

  • PDO: $pdo->quote($string) (but prefer prepared statements)
  • MySQLi: $mysqli->real_escape_string($string)

Note: real_escape_string() only works correctly if:

  • The connection charset is properly set (e.g., SET NAMES utf8mb4)
  • You’re using a supported MySQL client library
$mysqli->set_charset("utf8mb4");
$escaped = $mysqli->real_escape_string($userInput);
$query = "SELECT * FROM posts WHERE title = '$escaped'";

Still risky—only acceptable when prepared statements aren’t feasible.

2. Escaping Identifiers (Table/Column Names)

You can’t use placeholders for table names. Instead:

  • Use backticks (in MySQL) and escape them properly.
  • Whitelist allowed identifiers when possible.
function quoteIdentifier($identifier) {
    return '`' . str_replace('`', '``', $identifier) . '`';
}

$query = "SELECT * FROM " . quoteIdentifier($tableName);

Even better: avoid dynamic identifiers entirely or use a whitelist:

$allowedTables = ['users', 'posts', 'comments'];
if (!in_array($tableName, $allowedTables)) {
    die("Invalid table name");
}

3. Integers and Other Types

Always cast or validate:

$userId = (int)$input;
$query = "SELECT * FROM users WHERE id = $userId"; // Safe if cast

Or use prepared statements anyway:

$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([(int)$input]);

Summary: Defense-in-Depth with Context

Context Recommended Approach
String values in WHERE ? Prepared statements (best)
Dynamic table/columns ? Whitelist or quote with backticks
Integer inputs ? Cast to (int) or use prepared
Legacy code / edge cases ? Use real_escape_string + UTF-8 set
Never ? addslashes()

The key takeaway: escaping is not one-size-fits-all. The correct defense depends on where and how untrusted data enters your SQL query. Prepared statements handle most cases safely and should be your default. When they can’t be used, apply strict input validation, whitelisting, and proper escaping—with full awareness of the context.

Basically: drop addslashes(), use parameterized queries, and escape intelligently when you absolutely have to.

The above is the detailed content of Beyond `addslashes()`: Contextual Escaping for Robust SQL Injection Defense. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Navigating Backslash Hell: A Deep Dive into `preg_quote()` and Regex Escaping Navigating Backslash Hell: A Deep Dive into `preg_quote()` and Regex Escaping Jul 26, 2025 am 09:51 AM

preg_quote()escapesregex-specialcharacters,includingbackslashesandthedelimiter,totreatthemasliterals;2.avoiddouble-escapingbypassingrawstrings(e.g.,'C:\path')withoutpre-escapedbackslashes;3.useforwardslashesinpathswhenpossibletoreducebackslashclutter

Escape Character Behavior in PHP's Heredoc and Nowdoc Syntaxes Escape Character Behavior in PHP's Heredoc and Nowdoc Syntaxes Jul 26, 2025 am 09:45 AM

Heredoc handles variable interpolation and basic escape sequences such as \n, \t, \\, \$, but does not process \" or \', while Nowdoc does not perform variable interpolation and any escape processing. All contents, including \n and variables are output literally; 1. Variables such as $name will be replaced, \\n will be parsed as newlines; 2. $name and \n are kept as is true in Nowdoc; 3. No escape quotes are required for both; 4. The end identifier must occupy one line and no leading spaces. PHP7.3 allows the use of spaces to indent the end identifier. Therefore, Heredoc is suitable for multi-line strings that need to be formatted, and Nowdoc is suitable for outputting original content such as SQL or JavaScript.

Modern PHP Escaping Patterns for Secure and Clean Code Modern PHP Escaping Patterns for Secure and Clean Code Jul 26, 2025 am 09:51 AM

Alwaysescapeoutputusingcontext-specificmethods:htmlspecialchars()forHTMLcontentandattributes,rawurlencode()forURLs,andjson_encode()withJSON_HEX_TAG,JSON_HEX_APOS,JSON_HEX_QUOT,andJSON_UNESCAPED_UNICODEforJavaScript.2.UsetemplatingengineslikeTwig,Lara

Single vs. Double Quotes: A Definitive Guide to Escape Character Behavior Single vs. Double Quotes: A Definitive Guide to Escape Character Behavior Jul 28, 2025 am 04:44 AM

InBash,singlequotestreatallcharactersliterallywhiledoublequotesallowvariableexpansionandlimitedescaping;inPythonandJavaScript,bothquotetypeshandleescapesthesame,withthechoicemainlyaffectingreadabilityandconveniencewhenembeddingquotes,sousesinglequote

A Comparative Analysis: `addslashes()` vs. `htmlspecialchars()` vs. `mysqli_real_escape_string()` A Comparative Analysis: `addslashes()` vs. `htmlspecialchars()` vs. `mysqli_real_escape_string()` Jul 27, 2025 am 04:27 AM

addslashes() should be avoided for SQL escapes because it is not safe and not protected from SQL injection; htmlspecialchars() is used for HTML output to prevent XSS attacks; mysqli_real_escape_string() can be used for string escapes in MySQL queries, but is only a suboptimal option when preprocessing statements cannot be used. 1. addslashes() is outdated and unsafe and should not be used for SQL escape in modern applications; 2. htmlspecialchars() should be used when outputting user input and outputting to HTML to prevent XSS; 3. mysqli_real_escape_string(

Fortifying Your Views: The Critical Role of `htmlspecialchars()` in Preventing XSS Fortifying Your Views: The Critical Role of `htmlspecialchars()` in Preventing XSS Jul 29, 2025 am 04:57 AM

htmlspecialchars() is the primary line of defense against XSS attacks, converting special characters into HTML entities, ensuring that the content entered by the browser is treated as plain text rather than executable code. 1. When using it, you must specify character encoding (such as 'UTF-8') to avoid parsing vulnerabilities; 2. Always enable the ENT_QUOTES flag to escape single and double quotes to prevent injection in the property context; 3. It should be escaped at output rather than stored, avoid data solidification and repeated escape; 4. It cannot be relied on to defend against all XSS alone. It is necessary to process URLs in combination with urlencode(), json_encode() to process JavaScript data, and use HTMLP for rich text

The Art of the Backslash: Demystifying Escape Characters in PHP Regular Expressions The Art of the Backslash: Demystifying Escape Characters in PHP Regular Expressions Jul 27, 2025 am 03:18 AM

TomasterbackslashesinPHPregex,understandthattwolayersofparsingoccur:PHPprocessesescapesequencesfirst,thentheregexenginedoes;2.UsesinglequotesforregexpatternstoavoidPHPinterpretingescapeslike\basbackspace;3.Indoublequotes,doublethebackslashes(e.g.,&qu

The Escaped Escaper: Handling Literal Backslashes in PHP Strings and Paths The Escaped Escaper: Handling Literal Backslashes in PHP Strings and Paths Jul 26, 2025 am 09:35 AM

BackslashesgomissinginPHPbecausetheyaretreatedasescapecharactersindouble-quotedstrings,sotofixthis:1.Usesinglequotesforliteralpathslike'C:\Users\John\Documents',2.Ordoublethebackslashesindoublequotesas"C:\\Users\\\\John\\Documents",3.Prefer

See all articles