Setting the PHP magic quotes directive in an .htaccess file
The magic_quotes directive in PHP automatically escapes special characters (like quotes) in incoming data from forms, cookies, and URLs. While it was originally 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.
Steps to Configure magic_quotes in .htaccess
Section titled “Steps to Configure magic_quotes in .htaccess”Step 1: Access Your Hosting Account
Section titled “Step 1: Access Your Hosting Account”- Log in to cPanel or use FTP/File Manager.
- Navigate to your site’s root directory (e.g.,
/public_html).
Step 2: Create or Edit .htaccess
Section titled “Step 2: Create or Edit .htaccess”- If
.htaccessalready exists, open it for editing. - If not, create a new file named
.htaccess.
Step 3: Add the Directive
Section titled “Step 3: Add the Directive”Insert one of the following lines depending on your needs:
# Enable magic quotes (not recommended)php_flag magic_quotes_gpc On
# Disable magic quotes (recommended)php_flag magic_quotes_gpc OffStep 4: Save and Verify
Section titled “Step 4: Save and Verify”-
Save the
.htaccessfile. -
Create a test file
info.phpwith:<?php phpinfo(); ?> -
Load it in your browser and search for
magic_quotes_gpc. -
Confirm whether it is enabled or disabled.
-
Delete the test file afterward for security.
Best 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.