ArticleslgStudy

science

Magic quotes

Magic quotes is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Magic quotes rather than just read about it. In short: Magic quotes was a feature of the PHP scripting language, wherein strings are automatically escaped—special characters are prefixed with a backslash—before being passed on. It was introduced to help newcomers write functioning SQL commands without requiring manual escaping.

Key takeaways

  • Magic quotes belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Magic quotes to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Magic quotes from memory before moving on to harder problems.

Reference excerpt

Magic quotes was a feature of the PHP scripting language, wherein strings are automatically escaped—special characters are prefixed with a backslash—before being passed on. It was introduced to help newcomers write functioning SQL commands without requiring manual escaping. It was later described as intended to prevent inexperienced developers from writing code that was vulnerable to SQL injection attacks. This feature was officially deprecated as of PHP 5.3.0 and removed in PHP 5.4, due to security concerns.

Concept The current revision of the PHP manual mentions that the rationale behind magic quotes was to "help [prevent] code written by beginners from being dangerous." It was however originally introduced in PHP 2 as a php.h compile-time setting for msql, only escaping single quotes, "making it easier to pass form data directly to msql queries". It originally was intended as a "convenience feature, not as [a] security feature." The use scope for magic quotes was expanded in PHP 3. Single quotes, double quotes, backslashes and null characters in all user-supplied data all have a backslash prepended to them before being passed to the script in the $_GET, $_REQUEST, $_POST and $_COOKIE global variables. Developers can then in theory use string concatenation to construct safe SQL queries with data provided by the user. (This was most accurate when PHP 2 and PHP 3 were current, since the primary supported databases allowed only 1-byte character sets.)

Criticism Magic quotes were enabled by default in new installations of PHP 3 and 4, but could be disabled through the magic_quotes_gpc configuration directive. Since the operation of magic quotes was behind the scenes and not immediately obvious, developers may have been unaware of their existence and the potential problems that they could introduce. The PHP documentation pointed out several pitfalls and recommended that, despite being enabled by default, they should be disabled. Problems with magic quotes included:

Not all user supplied data is intended for insertion into a database. It may be rendered directly to the screen, stored in a session, or previewed before saving. This can result in backslashes being added where they are not wanted and being shown to the end user. This bug often creeped into even widely used software. Not all user supplied data used in database queries is obtained directly from sources protected by magic quotes. For instance, a user-supplied value might be inserted into a database, protected by magic quotes, and later retrieved from the database and used in a subsequent database operation. The latter use is not protected by magic quotes, and a naive programmer used to relying on them may be unaware of the need to protect it explicitly. Whatever protection magic quotes offer, they only work for quoted strings, leaving other query parts (such as numbers or column names) entirely unprotected, and giving the developer a false feeling of security. Magic quotes also use the generic functionality provided by PHP's addslashes() function, which is not Unicode-aware and is still subject to SQL injection vulnerabilities in some multi-byte character encodings. Database-specific functions such as mysql_real_escape_string() or, where possible, prepared queries with bound parameters, are preferred. While many database management systems support escaping quotes with a backslash, the standard actually calls for using another quote. Thus, magic quotes can only offer protection for databases configured to support escaping quotes with a backslash. Code written assuming magic quotes are disabled will not work in an environment where they are enabled or vice versa. This can be worked around using the ini_get() or ini_set() functions but requires forethought by programmers, and usage of ini_set() is generally seen as bad practice. Adding magic quotes and subsequently removing them where appropriate incurs a small but unnecessary amount of performance overhead. Magic quotes do not protect against other common, related, security vulnerabilities such as Cross-site scripting (XSS) or SMTP header injection attacks, that a beginner might assume are prevented by magic quotes. In November 2005, the core PHP developers decided that because of these problems, the magic quotes feature would be removed from PHP 6. When development of PHP 6 stalled and development continued on the 5.x branch instead, the feature was deprecated in PHP 5.3.0 and removed in 5.4.

Other approaches Some languages such as Perl and Ruby opt for an approach involving data tainting, where data from untrusted sources, such as user input, are considered "tainted" and can not be used for dangerous operations until explicitly marked as trustworthy, usually after validation or encoding. Since the construction of SQL queries is considered "dangerous" in this context, this forces the programmer to address the problem. Tainting does not solve the problem, but it does highlight those instances where there is a problem so that the programmer is able to solve them appropriately. Joel Spolsky has suggested using a form of Hungarian notation that indicates whether data are safe or unsafe. Modern database engines and libraries use parameterised queries to pass data to the database separately from SQL commands, greatly reducing the need to escape data before constructing the queries.

See also Leaning toothpick syndrome SQL Injection PHP

References

External links PHP manual on magic quotes Archived 2014-02-08 at the Wayback Machine

Worked examples

Example 1 — a first encounter with Magic quotes

Start with the simplest possible case. Write down what Magic quotes claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Magic quotes before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Magic quotes ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Magic quotes

In research
Magic quotes appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Magic quotes in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Magic quotes is common in secondary-school and first-year university syllabi. It links to neighbouring topics PHP, so understanding it makes those chapters shorter.
In everyday life
Look for Magic quotes outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Magic quotes” →

How to study Magic quotes in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Magic quotes means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Magic quotes out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Magic quotes in simple terms?

Magic quotes was a feature of the PHP scripting language, wherein strings are automatically escaped—special characters are prefixed with a backslash—before being passed on. It was introduced to help newcomers write functioning SQL commands without requiring manual escaping.

Why does Magic quotes matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Magic quotes?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Magic quotes.

Tags

  • PHP

Keep exploring