不推荐使用常量 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)