PHP防注入漏洞攻击的过滤函数代码
PHP整站防注入程序,需要在公共文件中require_once本文件,因为现在网站被注入攻击现象很严重,所以推荐大家使用,具体见如下代码。
<br /></p><p><?PHP </p>//PHP整站防注入程序,需要在公共文件中require_once本文件 <br />//判断magic_quotes_gpc状态 <br />if (@get_magic_quotes_gpc ()) { <br />$_GET = sec ( $_GET ); <br />$_POST = sec ( $_POST ); <br />$_COOKIE = sec ( $_COOKIE ); <br />$_FILES = sec ( $_FILES ); <br />} <br />$_SERVER = sec ( $_SERVER ); <br />function sec(&$array) { <br />//如果是数组,遍历数组,递归调用 <br />if (is_array ( $array )) { <br />foreach ( $array as $k => $v ) { <br />$array [$k] = sec ( $v ); <br />} <br />} else if (is_string ( $array )) { <br />//使用addslashes函数来处理 <br />$array = addslashes ( $array ); <br />} else if (is_numeric ( $array )) { <br />$array = intval ( $array ); <br />} <br />return $array; <br />} <br />//整型过滤函数 <br />function num_check($id) { <br />if (! $id) { <br />die ( '参数不能为空!' ); <br />} //是否为空的判断 <br />else if (inject_check ( $id )) { <br />die ( '非法参数' ); <br />} //注入判断 <br />else if (! is_numetic ( $id )) { <br />die ( '非法参数' ); <br />} <br />//数字判断 <br />$id = intval ( $id ); <br />//整型化 <br />return $id; <br />} <br />//字符过滤函数 <br />function str_check($str) { <br />if (inject_check ( $str )) { <br />die ( '非法参数' ); <br />} <br />//注入判断 <br />$str = htmlspecialchars ( $str ); <br />//转换html <br />return $str; <br />} <br />function search_check($str) { <br />$str = str_replace ( "_", "\_", $str ); <br />//把"_"过滤掉 <br />$str = str_replace ( "%", "\%", $str ); <br />//把"%"过滤掉 <br />$str = htmlspecialchars ( $str ); <br />//转换html <br />return $str; <br />} <br />//表单过滤函数 <br />function post_check($str, $min, $max) { <br />if (isset ( $min ) && strlen ( $str ) < $min) { <br />die ( '最少$min字节' ); <br />} else if (isset ( $max ) && strlen ( $str ) > $max) { <br />die ( '最多$max字节' ); <br />} <br />return stripslashes_array ( $str ); <br />} <br />//防注入函数 <br />function inject_check($sql_str) { <br />return eregi ( 'select|inert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|UNION|into|load_file|outfile', $sql_str ); <br />// www.scutephp.com 进行过滤,防注入 <br />} <br />function stripslashes_array(&$array) { <br />if (is_array ( $array )) { <br />foreach ( $array as $k => $v ) { <br />$array [$k] = stripslashes_array ( $v ); <br />} <br />} else if (is_string ( $array )) { <br />$array = stripslashes ( $array ); <br />} <br />return $array; <br />} <br />?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
