Fixing “Function get_magic_quotes_gpc() is deprecated” Error in CodeIgniter 3

Fixing “Function get_magic_quotes_gpc() is deprecated” Error in CodeIgniter 3

If you are working with CodeIgniter 3 and encounter the following error:

Message: Function get_magic_quotes_gpc() is deprecated

This issue arises when running your application on PHP 7.4 or later. The get_magic_quotes_gpc() function was deprecated in PHP 7.4 and completely removed in PHP 8.0. If your code relies on this function, it will generate a deprecation warning or even fail in newer PHP versions.

Why This Error Occurs

The get_magic_quotes_gpc() function was used in older versions of PHP to check if magic quotes were enabled. Magic quotes automatically escaped input data to prevent SQL injection, but this feature was removed due to security concerns and better alternative methods like prepared statements.

If your application includes a function like this in helpers/kgs_helper.php:

if (!function_exists('cleanQuery')) {            
  function cleanQuery($string) {
      //// prevents duplicate backslashes
      /*if(get_magic_quotes_gpc())  
      {
          $string = stripslashes2($string);
      }
      if (phpversion() >= '4.3.0')
      {
          $string = mysql_escape_mimic($string);
      }
      else
      {
          $string = mysql_escape_mimic($string);
      }*/
      
      $string = stripslashes2($string);
      return $string;
  } 
} // end 

The function get_magic_quotes_gpc() is now deprecated, and using it will cause issues.

Solution

To fix this error, simply remove the call to get_magic_quotes_gpc() and rely on stripslashes() only when necessary. Update your helper function as follows:

if (!function_exists('cleanQuery')) {            
  function cleanQuery($string) {
      // Remove unnecessary magic quotes check
      $string = stripslashes2($string);
      return $string;
  } 
} // end

If your application requires input sanitization, use modern approaches like prepared statements with PDO or MySQLi instead of manually escaping input.

Conclusion

By removing deprecated functions like get_magic_quotes_gpc(), you ensure your CodeIgniter 3 application remains compatible with newer PHP versions. Always follow best practices for input sanitization to keep your application secure and maintainable.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *