Maaz Zindani

Loading the index…

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

Deprecated: Constant FILTER_SANITIZE_STRING is deprecated

ErrorDeprecated: Constant FILTER_SANITIZE_STRING is deprecated in /var/www/app/form.php on line 12

Short answer

PHP 8.1 deprecated FILTER_SANITIZE_STRING and its alias FILTER_SANITIZE_STRIPPED. Replace each use by what you actually need: htmlspecialchars when the value is printed in HTML, or strip_tags when you want the tags removed before storing it. There is no one-line drop-in, because the old output cannot be matched exactly.

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

Fix it fast

  1. 1Find out which cause you have

    List every place the constants are used, so you replace each one by its purpose and not by a blind search and replace.

    grep -rn -E "FILTER_SANITIZE_(STRING|STRIPPED)|FILTER_FLAG_NO_ENCODE_QUOTES" --include=*.php .
  2. 2Match what you see, then jump to its fix

    1. 1Code like echo filter_var($_POST['name'], FILTER_SANITIZE_STRING)The value is printed back into an HTML pageFix
    2. 2Code like $title = filter_var($title, FILTER_SANITIZE_STRING) before an insertThe value should lose its tags before it is storedFix

What does the error look like?

  • Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /var/www/app/form.php on line 12
  • Deprecated: Constant FILTER_SANITIZE_STRIPPED is deprecated

Cause 1 of 2

The value is printed back into an HTML page

1Check

Find where the value ends up. If it is echoed into HTML, escaping at output is the job, not stripping on input.

grep -rn "FILTER_SANITIZE_STRING" --include=*.php . | grep -E "echo|print"

2Fix

Escape on output with htmlspecialchars and name the encoding. Since PHP 8.1 the defaults convert both quote types and replace invalid UTF-8 instead of returning an empty string.

echo htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8');

3Verify

Post a value with a tag and a quote in it. The page should show them as text.

php -r 'echo htmlspecialchars("<b>Tom</b> & \"Jerry\"", ENT_QUOTES, "UTF-8");'

Confirmed by php.net

Did this fix it?

Cause 2 of 2

The value should lose its tags before it is stored

1Check

The old filter stripped tags. strip_tags is the matching function. It is not an XSS defence: allowed tags keep all their attributes, and it strips only what looks like a tag.

grep -rn "FILTER_SANITIZE_STRING" --include=*.php . | grep -i -E "insert|save|store|update"

2Fix

Use strip_tags for the tag removal, keep the value in a prepared statement for the database, and still escape on output.

$title = strip_tags($_POST['title']);

3Verify

Pass a string with a tag through it and check what is left.

php -r 'echo strip_tags("<p>Hello <b>world</b></p>");'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Escape for the place the data goes: htmlspecialchars for HTML, prepared statements for SQL.
  • Check input against the type the field must be and reject what does not match.
  • Run the test suite with error_reporting set to E_ALL so deprecations show up before an upgrade.

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.