允许用户在 PHP 中提交 HTML

2024-02-11

我想允许大量用户提交 html 作为用户配置文件,我目前尝试过滤掉我不想要的内容,但我现在想更改并使用白名单方法。

这是我目前的非白名单方法

function FilterHTML($string) {
    if (get_magic_quotes_gpc()) {
        $string = stripslashes($string);
    }
    $string = html_entity_decode($string, ENT_QUOTES, "ISO-8859-1");
    // convert decimal
    $string = preg_replace('/&#(\d+)/me', "chr(\\1)", $string); // decimal notation
    // convert hex
    $string = preg_replace('/&#x([a-f0-9]+)/mei', "chr(0x\\1)", $string); // hex notation
    //$string = html_entity_decode($string, ENT_COMPAT, "UTF-8");
    $string = preg_replace('#(&\#*\w+)[\x00-\x20]+;#U', "$1;", $string);
    $string = preg_replace('#(<[^>]+[\s\r\n\"\'])(on|xmlns)[^>]*>#iU', "$1>", $string);
    //$string = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $string); //bad line
    $string = preg_replace('#/*\*()[^>]*\*/#i', "", $string); // REMOVE /**/
    $string = preg_replace('#([a-z]*)[\x00-\x20]*([\`\'\"]*)[\\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU', '...', $string); //JAVASCRIPT
    $string = preg_replace('#([a-z]*)([\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU', '...', $string); //VBSCRIPT
    $string = preg_replace('#([a-z]*)[\x00-\x20]*([\\\]*)[\\x00-\x20]*@([\\\]*)[\x00-\x20]*i([\\\]*)[\x00-\x20]*m([\\\]*)[\x00-\x20]*p([\\\]*)[\x00-\x20]*o([\\\]*)[\x00-\x20]*r([\\\]*)[\x00-\x20]*t#iU', '...', $string); //@IMPORT
    $string = preg_replace('#([a-z]*)[\x00-\x20]*e[\x00-\x20]*x[\x00-\x20]*p[\x00-\x20]*r[\x00-\x20]*e[\x00-\x20]*s[\x00-\x20]*s[\x00-\x20]*i[\x00-\x20]*o[\x00-\x20]*n#iU', '...', $string); //EXPRESSION
    $string = preg_replace('#</*\w+:\w[^>]*>#i', "", $string);
    $string = preg_replace('#</?t(able|r|d)(\s[^>]*)?>#i', '', $string); // strip out tables
    $string = preg_replace('/(potspace|pot space|rateuser|marquee)/i', '...', $string); // filter some words
    //$string = str_replace('left:0px; top: 0px;','',$string);
    do {
        $oldstring = $string;
        //bgsound|
        $string = preg_replace('#</*(applet|meta|xml|blink|link|script|iframe|frame|frameset|ilayer|layer|title|base|body|xml|AllowScriptAccess|big)[^>]*>#i', "...", $string);
    } while ($oldstring != $string);
    return addslashes($string);
}

上面的方法工作得很好,我使用了 2 年之后从未遇到过任何问题,但是对于白名单方法,有没有类似于 stackoverflow C# 方法但在 PHP 中的方法?http://refactormycode.com/codes/333-sanitize-html http://refactormycode.com/codes/333-sanitize-html


HTML Purifier http://htmlpurifier.org is a standards-compliant HTML filter library written in PHP. HTML Purifier will not only remove all malicious code (better known as XSS) with a thoroughly audited, secure yet permissive whitelist, it will also make sure your documents are standards compliant, something only achievable with a comprehensive knowledge of W3C's specifications.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

允许用户在 PHP 中提交 HTML 的相关文章

  • 设置大型电子邮件通知系统有哪些方法?

    我的公司有一个用 PHP 构建的网站 我们使用内置的 PHP 电子邮件功能每天向订阅者发送数千封电子邮件 这是一个糟糕的主意 它堵塞了我们的服务器 并且需要几个小时才能完成整个批次 现在我已经研究过像 MailChimp 这样的群发邮件服务
  • 如何将图像从 Android 应用程序上传到网络服务器的特定文件夹中

    如何将图像从 android 移动到 Web 服务器上的指定文件夹 这是我的安卓代码 package com example bitmaptest import java io ByteArrayOutputStream import ja
  • 所有 PHP 相等比较都是对称的吗?

    Is a b总是等价于 b a 我认为在 JavaScript 中 由于强制转换 有一些奇怪的情况并非如此 I think ide https stackoverflow com questions 4752579 are all php
  • Laravel Auth:attempt() 不会持久登录

    我在网上找到了许多有类似问题的资源 但似乎没有一个解决方案可以解决我的问题 当我使用以下代码登录用户时 一切看起来都很好 email Input get email password Input get password if Auth a
  • Smarty 如果 URL 包含

    使用 Smarty 标签我想确定 URL 是否包含单词 例如 if smarty get page contains product php 我知道 contains 不存在 但是我怎样才能轻松地编写类似的东西来实现上述代码呢 所有 PHP
  • 防止 Propel 插入空字符串

    当未设置列时 如何防止 Propel ORM 插入空字符串 CREATE TABLE user uid INTEGER PRIMARY KEY AUTO INCREMENT email VARCHAR 255 NOT NULL UNIQUE
  • 如何检测字符串中的非 ASCII 字符?

    如果我有一个 PHP 字符串 如何以有效的方式确定它是否至少包含一个非 ASCII 字符 我所说的非 ASCII 字符是指不属于该表的任何字符 http www asciitable com http www asciitable com
  • 媒体的 Google Cloud Storage 签名网址

    我已经建立了一个视频网站 为用户提供 m3u8 和关联的 ts 文件 我不希望媒体文件免费可用 所以我所做的是 当用户在网站上时 在 mysql 中使用他们的 IP 和令牌创建一个会话 当他们请求特定媒体子域 mp4 domain com
  • PHP 无法加载动态库“php_pdo_oci.dll”

    我在 Windows 8 上运行 Apache 2 4 7 和 PHP 5 5 9 我安装了 PHPUnit 并开始弹出此警告图像 警告 是的 我在 php ini 中启用了扩展加载以及 extension dir 以更正文件夹 并且该文件
  • 编辑 HTACCESS 文件以防止直接访问特定文件夹中的特定文件

    我试图阻止直接访问子文件夹中的特定文件 我意识到这个论坛上有很多描述类似问题的主题 但是 我的似乎有点尴尬 由于我已经存在 HTACCESS 文件 这是文件的文件路径 www example com PRINCIPAL PROJECTS m
  • 通过 URL 指定控制器类与为每个控制器编写一个脚本相比,有何优缺点?

    今年夏天我安装了两个不同的 PHP 系统 每个都使用两种不同的方法 方法 1 每个任务一个 PHP 文件 该方法需要一个PHP为每个主要任务创建文件 例如 我的上传脚本可以通过http www domain com upload php O
  • 访问sendBeacon发送的数据

    文档表明sendBeacon通过发送其数据HTTP POST request 但在 PHP 中 POST变量似乎是一个空数组 这是我的 JavaScript 代码 navigator sendBeacon beacon log php My
  • WordPress 包含 SVG 文件错误

    我使用 PHP 和 WordPress 在本地主机上 我可以毫无问题地包含 SVG 文件 但在实时服务器上 我尝试包含一个 SVG 文件以便能够使用 CSS 对其进行样式设置 我收到此错误消息 Parse error syntax erro
  • PHP 扩展开发入门 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 请推荐有关 PHP 低 级 modules 编程接口的帮助文章或教程 搜索我的书签 我发现的唯一链接是
  • Codeigniter - 出现 404 Not Found 错误

    我们在 godaddy 有两个托管套餐 我们的实时网站使用以下 htaccess 文件运行良好 无需在 url 中使用 index php 即可访问网站 RewriteEngine On RewriteCond REQUEST FILENA
  • 如何通过ssh检查ubuntu服务器上是否存在php和apache

    如何通过ssh检查Ubuntu服务器上apache是 否安装了php和mysql 另外如果安装的话在哪个目录 如果安装了其他软件包 例如 lighttpd 那么它在哪里 确定程序是否已安装的另一种方法是使用which命令 它将显示您正在搜索
  • 如何使用 php 在 sql 查询中转义引号?

    我有一个疑问 sql SELECT CustomerID FROM tblCustomer WHERE EmailAddress addslashes POST username AND Password addslashes POST p
  • Stream_context_set_params 不适用于 ssh2.sftp 包装器

    我想使用类似的功能here http www php net manual en function stream notification callback php 请检查以下代码 function notify notification
  • PHP递归遍历对象树[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • ZF3/2 - 如何捕获 EVENT_DISPATCH 侦听器中引发的异常?

    有什么方法可以在 EVENT DISPATCH 监听器中抛出异常吗 class Module public function onBootstrap EventInterface event application event gt get

随机推荐