Mysql security testing
一、没有进行预处理的SQL语句
<?php // 1.连接数据库 $conn = mysql_connect('127.0.0.1:3306', 'root', '518666'); if (!$conn) { die("Could not connect:" . mysql_error()); } // 2.选择数据库 mysql_select_db('mysql_safe', $conn); // 3.设置编码,注意这里是utf8而不是utf-8,如果写后者,MySQL不会识别的,会出现乱码的。 mysql_query("SET NAMES utf8"); $title = "我们的爱情"; $content = '你是/谁啊,大几\都"老梁"做做&>women<a>没'; $add_time = date("Y-m-d H:i:s"); // 转义字符 $content = mysql_real_escape_string($content); $content = htmlspecialchars($content, ENT_COMPAT); // 你是/谁啊,大几都做做&>women<a>没 // 自动过滤反斜杠 /* // 4.插入一条数据 $insert_sql = "insert into post_tbl (title, content, user_id, add_time) values ('{$title}', '{$content}', '4742551', '{$add_time}')"; if(mysql_query($insert_sql)) { echo 'ok'; } else { echo "Error : " . mysql_error(); } $ret = mysql_affected_rows(); print_r($ret); */ // 5.PDO预处理插入 // PDO(PHP Data Object)则是提供了一个 Abstraction Layer 来操作数据库 // 查询 $user_id = 174742; $password = "''or '1=1'" ; $sql = "select * from post_tbl where user_id = {$user_id} and password = {$password}"; print_r($sql); $query = mysql_query($sql); // $result = mysql_fetch_array($query); $rows = array(); while($row=mysql_fetch_array($query)) { $rows[] = $row; } print_r( $rows); // 关闭数据库连接 mysql_close($conn); /* $str = "Bill & 'Steve'"; echo htmlspecialchars($str, ENT_COMPAT); // 只转换双引号 echo "<br>"; echo htmlspecialchars($str, ENT_QUOTES); // 转换双引号和单引号 echo "<br>"; echo htmlspecialchars($str, ENT_NOQUOTES); // 不转换任何引号 */ /* 以上代码的 HTML 输出如下(查看源代码): <!DOCTYPE html> <html> <body> Bill & 'Steve'<br> Bill & 'Steve'<br> Bill & 'Steve' </body> </html> 以上代码的浏览器输出: Bill & 'Steve' Bill & 'Steve' Bill & 'Steve' */ function mforum_html_tag_to_html_entity($content) { $content = (string)trim($content); if(empty($content)) return ''; // $content = str_replace(' ', ' ', $content); $content = htmlspecialchars($content, ENT_COMPAT, GB2312, false); $content = str_replace(">", ">", $content); $content = str_replace("<", "<", $content); $content = str_replace("\"", """, $content); $content = preg_replace("/\\\$/", "$", $content); $content = preg_replace("/\r/", "", $content); $content = str_replace("!", "!", $content); $content = str_replace("'", "'", $content); $content = preg_replace("/\\\/", "\", $content); // 内容敏感词过滤 return $content; }
二、PDO处理的SQL语句
<?php // PDO的使用 // http://blog.csdn.net/qq635785620/article/details/11284591 $dbh = new PDO('mysql:host=127.0.0.1:3306;dbname=mysql_safe', 'root', '518666'); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->exec('set names utf8'); $title = "我们的爱情"; $content = '你是/谁啊,大几\都"老梁"做做&>women<a>没' . " 测试打印号'我是单引号'哈哈"; $user_id = 174742; $add_time = date("Y-m-d H:i:s"); // $insert_sql = "insert into post_tbl (title, content, user_id, add_time) values (:x_title, :x_content, :x_user_id, :x_add_time)"; // $stmt = $dbh->prepare($insert_sql); // $stmt->execute(array('x_title'=>$title,':x_content'=> $content, ':x_user_id' => $user_id, ':x_add_time' => $add_time)); // 查询 $user_id = "17474#"; // $password = "''or '1=1'"; $password = 123456; $sql = 'select * from post_tbl where user_id = :x_user_id and password = :x_password'; $stmt = $dbh->prepare($sql); $stmt->execute(array(':x_user_id'=>$user_id, ':x_password' => $password)); $rows = array(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $rows[] = $row; } print_r($rows); // echo $dbh->lastinsertid();

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











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.
