Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Old PHP code calls get_magic_quotes_gpc(), which no longer exists

ErrorDeprecated: Function get_magic_quotes_gpc() is deprecated

Short answer

get_magic_quotes_gpc() was deprecated in PHP 7.4 and removed in PHP 8.0, so old code that calls it throws a fatal undefined-function error on PHP 8. The function always returned false anyway. Delete the check and its stripslashes() branch, and protect database queries with prepared statements instead of manual escaping.

  • Applies to PHP 7.4, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every place the old function is used, so you can see which branch each one guards.

    grep -rn "get_magic_quotes" --include=*.php .
  2. 2Match what you see, then jump to its fix

    1. 1Call to undefined function get_magic_quotes_gpc() on PHP 8, or the Deprecated notice on 7.4.A guard around stripslashes() that cannot run any moreFix
    2. 2Queries are built by joining $_POST or $_GET values into the SQL string.The code relied on automatic slashes to keep SQL safeFix

What does the error look like?

  • Deprecated: Function get_magic_quotes_gpc() is deprecated
  • Fatal error: Uncaught Error: Call to undefined function get_magic_quotes_gpc()

Cause 1 of 2

A guard around stripslashes() that cannot run any more

1Check

The manual says the function was deprecated in 7.4.0, removed in 8.0.0, and always returns false. So the branch it guards is dead code.

grep -rn -B1 -A3 "get_magic_quotes_gpc" --include=*.php .

2Fix

Remove the check and the stripslashes() call it guards. Do not wrap it in function_exists(): the value would be false anyway.

// before
if (get_magic_quotes_gpc()) {
    $name = stripslashes($_POST['name']);
}
// after
$name = $_POST['name'];

3Verify

Search again: no call should be left, then reload the page that crashed.

grep -rn "get_magic_quotes" --include=*.php .

Confirmed by php.net

Did this fix it?

Cause 2 of 2

The code relied on automatic slashes to keep SQL safe

1Check

Magic quotes were never a safe way to build queries. Look for variables concatenated into SQL.

grep -rn "mysqli_query\|->query(" --include=*.php .

2Fix

Use a prepared statement. The mysqli manual says bound variables are sent to the server separately from the query, so they cannot interfere with it and need no escaping.

$stmt = $mysqli->prepare("INSERT INTO test(id, label) VALUES (?, ?)");
$stmt->bind_param("is", $id, $label);
$stmt->execute();

3Verify

Submit a value that contains a quote character such as O'Brien and confirm it is stored unchanged.

php -l path/to/file.php # syntax check only; then test the form by hand

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run old code through php -l and a static analyser before upgrading PHP.
  • Check the PHP manual's deprecated-function notes before every major PHP upgrade.
  • Escape for output (htmlspecialchars) where text goes into HTML, not on input.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.