Enabling and disabling the PHP magic quotes directive
The magic_quotes directive in PHP automatically escapes special characters (like quotes) in incoming data from forms, cookies, and URLs. While this was intended to help prevent SQL injection, it often caused unexpected behavior and was deprecated in PHP 5.3 and removed entirely in PHP 5.4 and later.
Important Notes
Section titled “Important Notes”magic_quotesis not available in PHP 5.4 and newer versions.- If you are running PHP 5.3 or earlier, you can still enable or disable it.
- Modern applications should use prepared statements and proper escaping instead of relying on
magic_quotes.
How to Enable or Disable magic_quotes
Section titled “How to Enable or Disable magic_quotes”1. Using a Custom php.ini File
Section titled “1. Using a Custom php.ini File”Edit or create a php.ini file in your application directory:
; Enable magic quotesmagic_quotes_gpc = On
; Disable magic quotesmagic_quotes_gpc = Off2. Using .htaccess
Section titled “2. Using .htaccess”On Apache servers, you can override the directive in .htaccess:
# Enablephp_flag magic_quotes_gpc On
# Disablephp_flag magic_quotes_gpc Off3. Using .user.ini
Section titled “3. Using .user.ini”If your hosting environment supports .user.ini, add:
magic_quotes_gpc = OffBest Practices
Section titled “Best Practices”- Always keep
magic_quotesdisabled in modern PHP versions. - Use parameterized queries (PDO or MySQLi) for database interactions.
- Avoid relying on automatic escaping, as it can break applications and cause double‑escaping issues.