PHP Xss 处理
PHP 处理 XSS
2023-09-14 08:57:36 时间
预防xss,页面中不同区域的字串应该使用不同的手段来处理,而不是一概的htmlspecialchars,简单+实用。
<?php /** * Xss 处理 * * 作者: 刘卫锋 (kevonliu@tencent.com) * 创建时间: 2013-07-04 * * $Id: Xss.php 48518 2013-07-05 03:35:04Z kevonliu $ */ class Xss { /** * 处理链接字串 * * @param string $str * @return string */ public static function link($str) { $pairs = array( '\'' => '%27', '"' => '%22', '<' => '%3C', '>' => '%3E', ); return strtr($str, $pairs); } /** * 处理js中的字串 * * @param string $str * @return string */ public static function js($str) { // See ECMA 262 section 7.8.4 for string literal format $pairs = array( // "\\" => "\\\\", // "\"" => "\\\"", // '\'' => '\\\'', '\"' => '\\x22', '\'' => '\\x39', '\\' => '\\x92', # To avoid closing the element or CDATA section '<' => '\\x3c', '>' => '\\x3e', # To avoid any complaints about bad entity refs '&' => '\\x26', # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152 # Encode certain Unicode formatting chars so affected # versions of Gecko don't misinterpret our strings; # this is a common problem with Farsi text. "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER ); return strtr($str, $pairs); } /** * 处理html中的字串 * * @param string $str * @return string */ public static function html($str) { $pairs = array( '"' => '"', '\'' => ''', '&' => '&', '>' => '>', '<' => '<', ); return strtr($str, $pairs); } } ?>
以上是php版本的代码,其他语言均类似。
欢迎指正,共同进步~~
http://521-wf.com/archives/221.html
相关文章
- PHP如何自动识别第三方Restful API的内容,自动渲染成 json、xml、html、serialize、csv、php等数据
- PHP中的错误处理、异常处理机制详解
- php ddos 安全处理代码
- PHP Socket编程 之 php中连接tcp服务的三种方式
- Docker 搭建多容器组合服务 (nginx + PHP)
- php面试题之PHP核心技术
- 通过yum安装nginx-mysql-php-fastcgi配置LNMP
- PHP实现迭代器
- PHP设计模式系列 - 观察者模式处理订单(异步操作附加功能)
- PHP Primary script unknown , File not found
- php:在linux上用sudo提升权限(centos 8 / PHP 7.4.2)
- php类库PHP QR Code 二维码
- PHP读取配置文件类(php,ini,yaml,xml)
- PHP QR Code - QR code generator, an LGPL PHP library
- [转]php rsa加密解密实例
- PHP+FastCGI+Nginx动态请求处理配置
- PHP:echo,print,print_r,var_dump区别
- php对uploads文件的处理问题的解决
- PHP获取POST数据的几种方法汇总
- php去除换行符的方法小结(PHP_EOL变量的使用)