


How to implement form submission data verification processing function in PHP
这篇文章主要介绍了PHP实现表单提交数据的验证处理功能,可实现防SQL注入和XSS攻击等,涉及php字符处理、编码转换相关操作技巧,需要的朋友可以参考下
防XSS攻击代码:
/** * 安全过滤函数 * * @param $string * @return string */ function safe_replace($string) { $string = str_replace('%20','',$string); $string = str_replace('%27','',$string); $string = str_replace('%2527','',$string); $string = str_replace('*','',$string); $string = str_replace('"','"',$string); $string = str_replace("'",'',$string); $string = str_replace('"','',$string); $string = str_replace(';','',$string); $string = str_replace('<','<',$string); $string = str_replace('>','>',$string); $string = str_replace("{",'',$string); $string = str_replace('}','',$string); $string = str_replace('\\','',$string); return $string; }
代码实例:
<?php $user_name = strim($_REQUEST['user_name']); function strim($str) { //trim() 函数移除字符串两侧的空白字符或其他预定义字符。 //htmlspecialchars() 函数把预定义的字符转换为 HTML 实体(防xss攻击)。 //预定义的字符是: //& (和号)成为 & //" (双引号)成为 " //' (单引号)成为 ' //< (小于)成为 < //> (大于)成为 > return quotes(htmlspecialchars(trim($str))); } //防sql注入 function quotes($content) { //if $content is an array if (is_array($content)) { foreach ($content as $key=>$value) { //$content[$key] = mysql_real_escape_string($value); /*addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。 预定义字符是: 单引号(') 双引号(") 反斜杠(\) NULL */ $content[$key] = addslashes($value); } } else { //if $content is not an array //$content=mysql_real_escape_string($content); $content=addslashes($content); } return $content; } ?>
//过滤sql注入 function filter_injection(&$request) { $pattern = "/(select[\s])|(insert[\s])|(update[\s])|(delete[\s])|(from[\s])|(where[\s])/i"; foreach($request as $k=>$v) { if(preg_match($pattern,$k,$match)) { die("SQL Injection denied!"); } if(is_array($v)) { filter_injection($request[$k]); } else { if(preg_match($pattern,$v,$match)) { die("SQL Injection denied!"); } } } }
防sql注入:
mysql_real_escape_string()
函数转义 SQL 语句中使用的字符串中的特殊字符。
下列字符受影响:
\x00
\n
\r
'
”
\x1a
如果成功,则该函数返回被转义的字符串。如果失败,则返回 false。
语法
mysql_real_escape_string(string,connection)
参数 | 描述 |
---|---|
string 必需。 | 规定要转义的字符串。 |
connection 可选。 | 规定 MySQL 连接。如果未规定,则使用上一个连接。 |
对于纯数字或数字型字符串的校验可以用
is_numeric()
检测变量是否为数字或数字字符串
实例:
<?php function get_numeric($val) { if (is_numeric($val)) { return $val + 0; } return 0; } ?>
is_array — 检测变量是否是数组 bool is_array ( mixed $var ) <br>
如果 var 是 array,则返回 TRUE,否则返回 FALSE。
is_dir 判断给定文件名是否是一个目录 bool is_dir ( string $filename ) <br>
判断给定文件名是否是一个目录。
如果文件名存在,并且是个目录,返回 TRUE,否则返回FALSE。
is_file — 判断给定文件名是否为一个正常的文件 bool is_file ( string $filename ) <br>
判断给定文件名是否为一个正常的文件。
如果文件存在且为正常的文件则返回 TRUE,否则返回 FALSE。
Note:
因为 PHP 的整数类型是有符号整型而且很多平台使用 32 位整型,对 2GB 以上的文件,一些文件系统函数可能返回无法预期的结果 。
is_bool — 检测变量是否是布尔型 bool is_bool ( mixed $var )<br>
如果 var 是 boolean 则返回 TRUE。
is_string — 检测变量是否是字符串 bool is_string ( mixed $var ) <br>
如果 var 是 string 则返回 TRUE,否则返回 FALSE。
is_int — 检测变量是否是整数 bool is_int ( mixed $var ) <br>
如果 var 是 integer 则返回 TRUE,否则返回 FALSE。
Note:
若想测试一个变量是否是数字或数字字符串(如表单输入,它们通常为字符串),必须使用 is_numeric()。
is_float — 检测变量是否是浮点型 bool is_float ( mixed $var ) <br>
如果 var 是 float 则返回 TRUE,否则返回 FALSE。
Note:
若想测试一个变量是否是数字或数字字符串(如表单输入,它们通常为字符串),必须使用 is_numeric()。
is_null — 检测变量是否为 NULL bool is_null ( mixed $var ) <br>
如果 var 是 null 则返回 TRUE,否则返回 FALSE。
is_readable — 判断给定文件名是否可读 bool is_readable ( string $filename )<br>
判断给定文件名是否存在并且可读。如果由 filename 指定的文件或目录存在并且可读则返回 TRUE,否则返回 FALSE。
is_writable — 判断给定的文件名是否可写 bool is_writable ( string $filename ) <br>
如果文件存在并且可写则返回 TRUE。filename 参数可以是一个允许进行是否可写检查的目录名。
file_exists — 检查文件或目录是否存在 bool file_exists ( string $filename ) <br>
检查文件或目录是否存在。
在 Windows 中要用 //computername/share/filename 或者 \computername\share\filename 来检查网络中的共享文件。
如果由 filename 指定的文件或目录存在则返回 TRUE,否则返回 FALSE。
is_executable — 判断给定文件名是否可执行 bool is_executable ( string $filename )<br>
判断给定文件名是否可执行。如果文件存在且可执行则返回 TRUE,错误时返回FALSE。
相关推荐:
解决php 处理 form 表单提交多个 name 属性值相同的 input 标签问题
The above is the detailed content of How to implement form submission data verification processing function in PHP. For more information, please follow other related articles on the PHP Chinese website!

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











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

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,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
