0

I am a beginner in php. I am running the final chapter project named Robin's Nest from the book "Learning PHP, MySQL and JavaScript with jQuery".

In the file function.php, the function sanitizeString raises the following deprecation error:

Deprecated: Function get_magic_quotes_gpc() is deprecated in C:\xampp2\htdocs\MyProjects\robinsnest\functions.php on line 42.

My version of php is 7.4.3 and I use Xampp on Windows 10 operating system. I checked the PHP manual for the function and saw that the function has been deprecated as of PHP 7.4.0.

How can I modify this function so that it doesn't use the deprecated function get_magic_quotes_gpc() and still get equivalent functionality as there seem to be no replacement function?

 function sanitizeString($var)
  {
    global $connection;
    $var = strip_tags($var);
    $var = htmlentities($var);
    if (get_magic_quotes_gpc())
      $var = stripslashes($var);
    return $connection->real_escape_string($var);
  }
halfer
  • 19,824
  • 17
  • 99
  • 186
Rilwan Smith
  • 23
  • 2
  • 6

1 Answers1

3

Just get rid of the code that uses the function. It hasn't been relevant for many versions.

 function sanitizeString($var)
  {
    global $connection;
    $var = strip_tags($var);
    $var = htmlentities($var);
    return $connection->real_escape_string($var);
  }

Note that this sanitization is not appropriate to begin with. htmlentities() should only be used when you're displaying data on a web page, not when you're processing input. And $connection->real_escape_string() should only be used when substituting variables into a SQL string, but it's better to use prepared statements for this.

Barmar
  • 741,623
  • 53
  • 500
  • 612