不推荐使用常量 FILTER_SANITIZE_STRING

Constant FILTER_SANITIZE_STRING is deprecated(不推荐使用常量 FILTER_SANITIZE_STRING)
本文介绍了不推荐使用常量 FILTER_SANITIZE_STRING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经安装了 PHP 8.1 并开始测试我的旧项目.我使用了过滤器 FILTER_SANITIZE_STRING 像这样:

I have installed PHP 8.1 and I started testing my old project. I have used the filter FILTER_SANITIZE_STRING like so:

$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);

现在我收到此错误:

已弃用:不推荐使用常量 FILTER_SANITIZE_STRING

Deprecated: Constant FILTER_SANITIZE_STRING is deprecated

当我使用 FILTER_SANITIZE_STRIPPED 时也会发生同样的情况:

The same happens when I use FILTER_SANITIZE_STRIPPED:

已弃用:不推荐使用常量 FILTER_SANITIZE_STRIPPED

Deprecated: Constant FILTER_SANITIZE_STRIPPED is deprecated

我可以用什么代替它?

推荐答案

这是一个用途可疑的过滤器.很难说它究竟要完成什么任务或何时应该使用它.由于其名称,它也与默认字符串过滤器混淆,而实际上默认字符串过滤器称为 FILTER_UNSAFE_RAW.PHP 社区决定不再支持使用此过滤器.

This was a filter of dubious purpose. It's difficult to say what it was meant to accomplish exactly or when it should be used. It was also confused with the default string filter, due to its name, when in reality the default string filter is called FILTER_UNSAFE_RAW. The PHP community decided that the usage of this filter should not be supported anymore.

此过滤器的行为非常不直观.它删除了 < 和字符串结尾之间或直到下一个 > 之间的所有内容.它还删除了所有 NUL 字节.最后,它将 '" 编码到它们的 HTML 实体中.

The behaviour of this filter was very unintuitive. It removed everything between < and the end of the string or until the next >. It also removed all NUL bytes. Finally, it encoded ' and " into their HTML entities.

如果你想更换它,你有几个选择:

If you want to replace it, you have a couple of options:

  1. 使用不进行任何过滤的默认字符串过滤器 FILTER_UNSAFE_RAW.如果您对 FILTER_SANITIZE_STRING 的行为一无所知,而您只想使用一个默认过滤器来为您提供字符串值,则应该使用它.

  1. Use the default string filter FILTER_UNSAFE_RAW that doesn't do any filtering. This should be used if you had no idea about the behaviour of FILTER_SANITIZE_STRING and you just want to use a default filter that will give you the string value.

如果您使用此过滤器来防御 XSS 漏洞,请将其替换为 htmlspecialchars().不要在输入数据上调用此函数.为了防止 XSS,您需要对输出进行编码!

If you used this filter to protect against XSS vulnerabilities, then replace its usage with htmlspecialchars(). Don't call this function on the input data. To protect against XSS you need to encode the output!

如果您确切地知道该过滤器的作用并且想要创建一个 polyfill,则可以使用正则表达式轻松完成.

If you knew exactly what that filter does and you want to create a polyfill, you can do that easily with regex.

function filter_string_polyfill(string $string): string
{
    $str = preg_replace('/x00|<[^>]*>?/', '', $string);
    return str_replace(["'", '"'], ['&#39;', '&#34;'], $str);
}

这篇关于不推荐使用常量 FILTER_SANITIZE_STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Subtract time in PHP(在 PHP 中减去时间)
Limit execution time of an function or command PHP(限制函数或命令 PHP 的执行时间)
How to sum N number of time (HH:MM Format)?(如何求和 N 次(HH:MM 格式)?)
How to get current time in milliseconds in PHP?(如何在 PHP 中以毫秒为单位获取当前时间?)
How to check if time is between two times in PHP(如何检查时间是否在 PHP 中的两次之间)
Convert number of minutes into hours amp; minutes using PHP(将分钟数转换为小时数分钟使用 PHP)