Cloud Service >> Knowledgebase >> Domains & DNS >> How to turn magic_quotes_gpc off
submit query

Cut Hosting Costs! Submit Query Today!

How to turn magic_quotes_gpc off

Introduction:

magic_quotes_gpc was a feature in PHP designed to automatically escape certain characters in GET, POST, and COOKIE data to help prevent SQL injection. However, it was deprecated in PHP 5.3.0 and removed entirely in PHP 5.4.0 due to security and performance concerns. It is important to ensure that magic_quotes_gpc is turned off in any modern PHP application. This guide will walk you through the steps to disable magic_quotes_gpc.

 


 

Step 1: Check if magic_quotes_gpc is Enabled

Before disabling magic_quotes_gpc, you may want to verify whether it is currently enabled.

1. Create a PHP Info Page:

- Create a file named phpinfo.php in your web root directory.

- Add the following content to the file:

- Access this file via your web browser (e.g., http://yourdomain.com/phpinfo.php).

2. Search for magic_quotes_gpc:

- Look for the magic_quotes_gpc directive in the output.

- If it shows On, it means magic_quotes_gpc is enabled.

Step 2: Disable magic_quotes_gpc via php.ini

The most effective way to disable magic_quotes_gpc is by modifying the php.ini configuration file.

1. Locate the php.ini File:

- The location of the php.ini file depends on your server setup. Common locations include /etc/php.ini, /etc/php/7.x/apache2/php.ini, or /usr/local/lib/php.ini.

2. Edit the php.ini File:

- Open the php.ini file in a text editor.

- Search for the line that reads:
php.ini File

 

- Change it to:
php.ini File

3. Restart the Web Server:

- After saving the changes to php.ini, you need to restart your web server for the changes to take effect.

- For Apache, you can use:
php.ini File

- For Nginx with PHP-FPM, use:
php.ini File

Step 3: Disable magic_quotes_gpc via .htaccess (Alternative Method)

If you do not have access to the php.ini file, you can try disabling magic_quotes_gpc using a .htaccess file.

1. Create/Edit the .htaccess File:

- In your web root directory, open or create a .htaccess file.

  2. Add the Following Directive:
.htaccess File

3. Check if the Change Took Effect:

- Use the phpinfo.php file created earlier to verify that magic_quotes_gpc is now set to Off.

Step 4: Handle Escaped Data in Legacy Applications

If your application was written with magic_quotes_gpc in mind, simply turning it off might cause issues since data will no longer be automatically escaped. You will need to manually handle escaping where necessary.

1. Manually Strip Slashes:

- If your code expects slashes added by magic_quotes_gpc, you should use stripslashes() to remove them.

- Example:
 Escaped Data in Legacy Applications

2. Use Modern PHP Security Practices:

- Instead of relying on magic_quotes_gpc, use prepared statements with parameterized queries to protect against SQL injection.

 


 

Note:

magic_quotes_gpc is an outdated feature that should be turned off in modern PHP environments to avoid potential security risks and performance issues. By following the steps outlined in this guide, you can ensure that your PHP setup is configured correctly. Additionally, transitioning your code to use modern security practices, such as prepared statements, will provide better protection and compatibility with newer PHP versions.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!