search
Homephp教程PHP源码Simple php anti-injection and anti-cross-site functions

function fn_safe($str_string) {
    //直接剔除
    $_arr_dangerChars = array(
        "|", ";", "$", "@", "+", "\t", "\r", "\n", ",", "(", ")", PHP_EOL //特殊字符
    );
 
    //正则剔除
    $_arr_dangerRegs = array(
        /* -------- 跨站 --------*/
 
        //html 标签
        "/<(script|frame|iframe|bgsound|link|object|applet|embed|blink|style|layer|ilayer|base|meta)\s+\S*>/i",
 
        //html 属性
        "/on(afterprint|beforeprint|beforeunload|error|haschange|load|message|offline|online|pagehide|pageshow|popstate|redo|resize|storage|undo|unload|blur|change|contextmenu|focus|formchange|forminput|input|invalid|reset|select|submit|keydown|keypress|keyup|click|dblclick|drag|dragend|dragenter|dragleave|dragover|dragstart|drop|mousedown|mousemove|mouseout|mouseover|mouseup|mousewheel|scroll|abort|canplay|canplaythrough|durationchange|emptied|ended|error|loadeddata|loadedmetadata|loadstart|pause|play|playing|progress|ratechange|readystatechange|seeked|seeking|stalled|suspend|timeupdate|volumechange|waiting)\s*=\s*(\"|&#39;)?\S*(\"|&#39;)?/i",
 
        //html 属性包含脚本
        "/\w+\s*=\s*(\"|&#39;)?(java|vb)script:\S*(\"|&#39;)?/i",
 
        //js 对象
        "/(document|location)\s*\.\s*\S*/i",
 
        //js 函数
        "/(eval|alert|prompt|msgbox)\s*\(.*\)/i",
 
        //css
        "/expression\s*:\s*\S*/i",
 
        /* -------- sql 注入 --------*/
 
        //显示 数据库 | 表 | 索引 | 字段
        "/show\s+(databases|tables|index|columns)/i",
 
        //创建 数据库 | 表 | 索引 | 视图 | 存储过程 | 存储过程
        "/create\s+(database|table|(unique\s+)?index|view|procedure|proc)/i",
 
        //更新 数据库 | 表
        "/alter\s+(database|table)/i",
 
        //丢弃 数据库 | 表 | 索引 | 视图 | 字段
        "/drop\s+(database|table|index|view|column)/i",
 
        //备份 数据库 | 日志
        "/backup\s+(database|log)/i",
 
        //初始化 表
        "/truncate\s+table/i",
 
        //替换 视图
        "/replace\s+view/i",
 
        //创建 | 更改 字段
        "/(add|change)\s+column/i",
 
        //选择 | 更新 | 删除 记录
        "/(select|update|delete)\s+\S*\s+from/i",
 
        //插入 记录 | 选择到文件
        "/insert\s+into/i",
 
        //sql 函数
        "/load_file\s*\(.*\)/i",
 
        //sql 其他
        "/(outfile|infile)\s+(\"|&#39;)?\S*(\"|&#39;)/i",
    );
 
    $_str_return = $str_string;
    //$_str_return = urlencode($_str_return);
 
    foreach ($_arr_dangerChars as $_key=>$_value) {
        $_str_return = str_ireplace($_value, "", $_str_return);
    }
 
    foreach ($_arr_dangerRegs as $_key=>$_value) {
        $_str_return = preg_replace($_value, "", $_str_return);
    }
 
    $_str_return = htmlentities($_str_return, ENT_QUOTES, "UTF-8", true);
 
    return $_str_return;
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)