Home Backend Development PHP Tutorial php 保险过滤函数代码

php 保险过滤函数代码

Jun 13, 2016 pm 12:28 PM
controller replace return string

php 安全过滤函数代码

php 安全过滤函数代码,防止用户恶意输入内容。

<span style="color: #008000;">//</span><span style="color: #008000;">安全过滤输入[jb]</span>function check_str($<span style="color: #0000ff;">string</span>, $isurl = <span style="color: #0000ff;">false</span><span style="color: #000000;">){$</span><span style="color: #0000ff;">string</span> = preg_replace(<span style="color: #800000;">'</span><span style="color: #800000;">/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/</span><span style="color: #800000;">'</span>,<span style="color: #800000;">''</span>,$<span style="color: #0000ff;">string</span><span style="color: #000000;">);$</span><span style="color: #0000ff;">string</span> = str_replace(array(<span style="color: #800000;">"</span><span style="color: #800000;">\0</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">%00</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">\r</span><span style="color: #800000;">"</span>),<span style="color: #800000;">''</span>,$<span style="color: #0000ff;">string</span><span style="color: #000000;">);empty($isurl) </span>&& $<span style="color: #0000ff;">string</span> = preg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">/&(?!(#[0-9]+|[a-z]+);)/si</span><span style="color: #800000;">"</span>,<span style="color: #800000;">'</span><span style="color: #800000;">&</span><span style="color: #800000;">'</span>,$<span style="color: #0000ff;">string</span><span style="color: #000000;">);$</span><span style="color: #0000ff;">string</span> = str_replace(array(<span style="color: #800000;">"</span><span style="color: #800000;">%3C</span><span style="color: #800000;">"</span>,<span style="color: #800000;">'</span><span style="color: #800000;"><span style="color: #800000;">'</span>),<span style="color: #800000;">'</span><span style="color: #800000;"><span style="color: #800000;">'</span>,$<span style="color: #0000ff;">string</span><span style="color: #000000;">);$</span><span style="color: #0000ff;">string</span> = str_replace(array(<span style="color: #800000;">"</span><span style="color: #800000;">%3E</span><span style="color: #800000;">"</span>,<span style="color: #800000;">'</span><span style="color: #800000;">></span><span style="color: #800000;">'</span>),<span style="color: #800000;">'</span><span style="color: #800000;">></span><span style="color: #800000;">'</span>,$<span style="color: #0000ff;">string</span><span style="color: #000000;">);$</span><span style="color: #0000ff;">string</span> = str_replace(array(<span style="color: #800000;">'</span><span style="color: #800000;">"</span><span style="color: #800000;">'</span>,<span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span>,<span style="color: #800000;">'</span> <span style="color: #800000;">'</span>),array(<span style="color: #800000;">'</span><span style="color: #800000;">“</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">‘</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span> <span style="color: #800000;">'</span>,<span style="color: #800000;">'</span> <span style="color: #800000;">'</span>),$<span style="color: #0000ff;">string</span><span style="color: #000000;">);</span><span style="color: #0000ff;">return</span> trim($<span style="color: #0000ff;">string</span><span style="color: #000000;">);}</span></span></span>
Copy after login

下面是整理的一些过滤函数:

<span style="color: #008000;">/*</span><span style="color: #008000;">** 安全过滤类-过滤javascript,css,iframes,object等不安全参数 过滤级别高*  Controller中使用方法:$this->controller->fliter_script($value)* @param  string $value 需要过滤的值* @return string</span><span style="color: #008000;">*/</span><span style="color: #000000;">function fliter_script($value) {$value </span>= preg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">&111n\\2</span><span style="color: #800000;">"</span><span style="color: #000000;">,$value);$value </span>= preg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">/(.*?)/si</span><span style="color: #800000;">"</span>,<span style="color: #800000;">""</span><span style="color: #000000;">,$value);$value </span>= preg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">/(.*?)/si</span><span style="color: #800000;">"</span>,<span style="color: #800000;">""</span><span style="color: #000000;">,$value);$value </span>= preg_replace (<span style="color: #800000;">"</span><span style="color: #800000;">//iesU</span><span style="color: #800000;">"</span>, <span style="color: #800000;">''</span><span style="color: #000000;">, $value);</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $value;}</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 安全过滤类-过滤HTML标签*  Controller中使用方法:$this->controller->fliter_html($value)* @param  string $value 需要过滤的值* @return string</span><span style="color: #008000;">*/</span><span style="color: #000000;">function fliter_html($value) {</span><span style="color: #0000ff;">if</span> (function_exists(<span style="color: #800000;">'</span><span style="color: #800000;">htmlspecialchars</span><span style="color: #800000;">'</span>)) <span style="color: #0000ff;">return</span><span style="color: #000000;"> htmlspecialchars($value);</span><span style="color: #0000ff;">return</span> str_replace(array(<span style="color: #800000;">"</span><span style="color: #800000;">&</span><span style="color: #800000;">"</span>, <span style="color: #800000;">'</span><span style="color: #800000;">"</span><span style="color: #800000;">'</span>, <span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;"><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">></span><span style="color: #800000;">"</span>), array(<span style="color: #800000;">"</span><span style="color: #800000;">&</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">\"</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;"><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">></span><span style="color: #800000;">"</span><span style="color: #000000;">), $value);}</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 安全过滤类-对进入的数据加下划线 防止SQL注入*  Controller中使用方法:$this->controller->fliter_sql($value)* @param  string $value 需要过滤的值* @return string</span><span style="color: #008000;">*/</span><span style="color: #000000;">function fliter_sql($value) {$sql </span>= array(<span style="color: #800000;">"</span><span style="color: #800000;">select</span><span style="color: #800000;">"</span>, <span style="color: #800000;">'</span><span style="color: #800000;">insert</span><span style="color: #800000;">'</span>, <span style="color: #800000;">"</span><span style="color: #800000;">update</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">delete</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">\'</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">\/\*</span><span style="color: #800000;">"</span><span style="color: #000000;">,     </span><span style="color: #800000;">"</span><span style="color: #800000;">\.\.\/</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">\.\/</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">union</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">into</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">load_file</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">outfile</span><span style="color: #800000;">"</span><span style="color: #000000;">);$sql_re </span>= array(<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span>,<span style="color: #800000;">""</span><span style="color: #000000;">);</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> str_replace($sql, $sql_re, $value);}</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 安全过滤类-通用数据过滤*  Controller中使用方法:$this->controller->fliter_escape($value)* @param string $value 需要过滤的变量* @return string|array</span><span style="color: #008000;">*/</span><span style="color: #000000;">function fliter_escape($value) {</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (is_array($value)) {  </span><span style="color: #0000ff;">foreach</span> ($value <span style="color: #0000ff;">as</span> $k =><span style="color: #000000;"> $v) {   $value[$k] </span>=<span style="color: #000000;"> self::fliter_str($v);  }} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {  $value </span>=<span style="color: #000000;"> self::fliter_str($value);}</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $value;}</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 安全过滤类-字符串过滤 过滤特殊有危害字符*  Controller中使用方法:$this->controller->fliter_str($value)* @param  string $value 需要过滤的值* @return string</span><span style="color: #008000;">*/</span><span style="color: #000000;">function fliter_str($value) {$badstr </span>= array(<span style="color: #800000;">"</span><span style="color: #800000;">\0</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">%00</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">\r</span><span style="color: #800000;">"</span>, <span style="color: #800000;">'</span><span style="color: #800000;">&</span><span style="color: #800000;">'</span>, <span style="color: #800000;">'</span> <span style="color: #800000;">'</span>, <span style="color: #800000;">'</span><span style="color: #800000;">"</span><span style="color: #800000;">'</span>, <span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;"><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">></span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span>   <span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">%3C</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">%3E</span><span style="color: #800000;">"</span><span style="color: #000000;">);$newstr </span>= array(<span style="color: #800000;">''</span>, <span style="color: #800000;">''</span>, <span style="color: #800000;">''</span>, <span style="color: #800000;">'</span><span style="color: #800000;">&</span><span style="color: #800000;">'</span>, <span style="color: #800000;">'</span> <span style="color: #800000;">'</span>, <span style="color: #800000;">'</span><span style="color: #800000;">"</span><span style="color: #800000;">'</span>, <span style="color: #800000;">'''</span><span style="color: #800000;">, "", "   ", "");</span>$value  =<span style="color: #000000;"> str_replace($badstr, $newstr, $value);$value  </span>= preg_replace(<span style="color: #800000;">'</span><span style="color: #800000;">/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/</span><span style="color: #800000;">'</span>, <span style="color: #800000;">'</span><span style="color: #800000;">&\\1</span><span style="color: #800000;">'</span><span style="color: #000000;">, $value);</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $value;}</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 私有路劲安全转化*  Controller中使用方法:$this->controller->filter_dir($fileName)* @param string $fileName* @return string</span><span style="color: #008000;">*/</span><span style="color: #000000;">function filter_dir($fileName) {$tmpname </span>=<span style="color: #000000;"> strtolower($fileName);$temp </span>= array(<span style="color: #800000;">'</span><span style="color: #800000;">:/</span><span style="color: #800000;">'</span>,<span style="color: #800000;">"</span><span style="color: #800000;">\0</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">..</span><span style="color: #800000;">"</span><span style="color: #000000;">);</span><span style="color: #0000ff;">if</span> (str_replace($temp, <span style="color: #800000;">''</span>, $tmpname) !==<span style="color: #000000;"> $tmpname) {  </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;}</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $fileName;}</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 过滤目录*  Controller中使用方法:$this->controller->filter_path($path)* @param string $path* @return array</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> function filter_path($path) {$path </span>= str_replace(array(<span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span>,<span style="color: #800000;">'</span><span style="color: #800000;">#</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">=</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">`</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">$</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">%</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">&</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">;</span><span style="color: #800000;">'</span>), <span style="color: #800000;">''</span><span style="color: #000000;">, $path);</span><span style="color: #0000ff;">return</span> rtrim(preg_replace(<span style="color: #800000;">'</span><span style="color: #800000;">/(\/){2,}|(\\\){1,}/</span><span style="color: #800000;">'</span>, <span style="color: #800000;">'</span><span style="color: #800000;">/</span><span style="color: #800000;">'</span>, $path), <span style="color: #800000;">'</span><span style="color: #800000;">/</span><span style="color: #800000;">'</span><span style="color: #000000;">);}</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 过滤PHP标签*  Controller中使用方法:$this->controller->filter_phptag($string)* @param string $string* @return string</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">public</span> function filter_phptag($<span style="color: #0000ff;">string</span><span style="color: #000000;">) {</span><span style="color: #0000ff;">return</span> str_replace(array(<span style="color: #800000;">''</span>), array(<span style="color: #800000;">'</span><span style="color: #800000;"></span><span style="color: #800000;">'</span>, <span style="color: #800000;">'</span><span style="color: #800000;">?></span><span style="color: #800000;">'</span>), $<span style="color: #0000ff;">string</span><span style="color: #000000;">);}</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 安全过滤类-返回函数*  Controller中使用方法:$this->controller->str_out($value)* @param  string $value 需要过滤的值* @return string</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> function str_out($value) {$badstr </span>= array(<span style="color: #800000;">"</span><span style="color: #800000;"><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">></span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">%3C</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">%3E</span><span style="color: #800000;">"</span><span style="color: #000000;">);$newstr </span>= array(<span style="color: #800000;">"</span><span style="color: #800000;"><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">></span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;"><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">></span><span style="color: #800000;">"</span><span style="color: #000000;">);$value  </span>=<span style="color: #000000;"> str_replace($newstr, $badstr, $value);</span><span style="color: #0000ff;">return</span> stripslashes($value); <span style="color: #008000;">//</span><span style="color: #008000;">下划线</span>}</span></span></span></span></span></span>
Copy after login

 

Statement of this Website
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

How to solve the problem that SpringBoot cannot scan the Controller How to solve the problem that SpringBoot cannot scan the Controller May 14, 2023 am 08:10 AM

When a SpringBoot novice creates a project, the Controller cannot be scanned for a series of problems 1.2.3.4.5.6. Another way is to add @ComponentScan(basePackages={"xxx.xxx.xx","xxx.xxx" when starting the service class) .xx”}) is the fully qualified name of the package, which can be used for multiple SpringBoot custom controllers. The SpringBoot custom controller route cannot be scanned and cannot be found because the startup class and the custom Controller package are not in the same directory. Officially recommended placement of application.java

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

How to add URL prefix to SpringBoot multiple controllers How to add URL prefix to SpringBoot multiple controllers May 12, 2023 pm 06:37 PM

Preface In some cases, the prefixes in the service controller are consistent. For example, the prefix of all URLs is /context-path/api/v1, and a unified prefix needs to be added to some URLs. The conceivable solution is to modify the context-path of the service and add api/v1 to the context-path. Modifying the global prefix can solve the above problem, but there are disadvantages. If the URL has multiple prefixes, for example, some URLs require prefixes. If it is api/v2, it cannot be distinguished. If you do not want to add api/v1 to some static resources in the service, it cannot be distinguished. The following uses custom annotations to uniformly add certain URL prefixes. one,

See all articles