防止sql注入
在网上看了些关于防止sql注入的文件和提问,比如使用预处理、过滤敏感字符。我一直想不明白的是,解决ssql注入问题非常非常简单啊,只要对用户输入的内容转义一下就可以了,就是讲用户输入的',"转义成\',\"就可以了,搞不懂为什么搞的那么复杂了?还是说我这种方法不能杜绝所有的sql注入
回复内容:
在网上看了些关于防止sql注入的文件和提问,比如使用预处理、过滤敏感字符。我一直想不明白的是,解决ssql注入问题非常非常简单啊,只要对用户输入的内容转义一下就可以了,就是讲用户输入的',"转义成\',\"就可以了,搞不懂为什么搞的那么复杂了?还是说我这种方法不能杜绝所有的sql注入
用htmlspecialchars/HTMLPurifier防御XSS注入,用预处理参数化查询防御SQL注入.
调用HTMLPurifier过滤XSS后输出HTML:
<code>require dirname(__FILE__).'/htmlpurifier/library/HTMLPurifier.auto.php'; $purifier = new HTMLPurifier(); echo $purifier->purify($html); </code>
MySQLi绑定参数查询:
<code>$db = @new mysqli(); $stmt = $db->prepare('SELECT * FROM posts WHERE id=?'); //预处理 $stmt->bind_param('i', $id); //绑定参数 $stmt->execute(); //查询 var_export($stmt->get_result()->fetch_all()); </code>
WireShark里用tcp.port==3306过滤分析PHP和MySQL通信
<code><?php //MySQLi $id = 1; $mysqli = new mysqli('127.0.0.1', 'punbb', 'punbb', 'punbb'); $mysqli->set_charset('utf8'); $stmt = $mysqli->prepare("SELECT `username` FROM `pb_users` WHERE `id`=?"); $stmt->bind_param('i', $id); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($username); while ($stmt->fetch()) echo $username; $stmt->close(); $mysqli->close(); <?php //PDO $id = 1; //$dbh = new PDO('sqlite:/path/to/punbb.db3'); $dsn = "mysql:dbname=punbb;host=127.0.0.1;port=3306;charset=utf8"; $dbh = new PDO($dsn, 'punbb', 'punbb'); $dbh->query('SET NAMES utf8'); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $sth = $dbh->prepare("SELECT `username` FROM `pb_users` WHERE `id`=?"); $sth->bindParam(1, $id, PDO::PARAM_INT); $sth->execute(); print_r($sth->fetchAll(PDO::FETCH_ASSOC)); $sth = null; $dbh = null; </code>
在 Request Prepare Statement 里可以看到 SELECT username
FROM pb_users
WHERE id
=?
在 Request Execute Statement 里可以看到 Parameter 内容为:
Type: FIELD_TYPE_LONGLONG (8)
Unsigned: 0
Value: 1
可见PHP将SQL模板和变量分两次发送给MySQL,由MySQL完成变量的转义处理.
既然SQL模板和变量是分两次发送的,那么就不存在SQL注入的问题了.
在MySQL的general_log里可以看到:
Prepare SELECT username
FROM pb_users
WHERE id
=?
Execute SELECT username
FROM pb_users
WHERE id
=1
如果ID绑定为string,则Execute时id赋值是这样的: id
='1'
如果PDO没有关闭模拟预处理,则可以看到:
Query SELECT username
FROM pb_users
WHERE id
=1
解决注入,还是PDO来的比较彻底,最不济也得mysqli,上bind,加类型.
只要对用户输入的内容转义一下就可以了,就是讲用户输入的',"转义成\',\"就可以了,搞不懂为什么搞的那么复杂了?还是说我这种方法不能杜绝所有的sql注入
你一定不知道有一种注入叫做宽字符注入,int型注入,orderBy注入,
其次注入其实非常容易防御,主要麻烦的是xss

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

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 still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

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.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
