PHP 关于SQL注入的防范措施。
最近在使用框架的时候还是有点不安,不知道框架的设计者有没有考虑到SQL-Injection的问题,我在顶层需不需要做一些必要的过滤等等,由此我特意的去StackOverflow看了下,真是获益良多,然后我去看了下框架的DB库的内部方法,然后就比较安心了。分享下国内外PHP程序员在处理SQL-Injection的一些方案。
国外普遍都推荐,只要你是使用查询都应该做到两点:1.prepared statements(准备好的声明) 2.parameterized queries (参数化的查询请求)。
我一开始也不理解这个是什么意思,后来看他们举例就大概知道了。比较安全的SQL,你需要一开始对查询的变量进行准备。如:
$name = $_POST['name'];$sql = 'select * from user where name'.$name;
那么最好就是对$name先处理下,
$name = mysql_real_escape_string($_POST['name']);
然后,让请求过来的变量成为参数,而不是SQL语言本身。
$sql = 'select * from user where name=\''.$name.'\'';
当然,这种写法还是比较粗糙。
所以,一般都会推荐使用PDO 或者是MYSQLI的prepare() excute()方法。
$stmt = $pdo->prepare('SELECT * FROM user WHERE name = :name');$stmt->execute(array('name' => $name));
关于PDO::prepare()
这样做的好处就是,你不再需要担心查询请求会插入一些SQL语句,因为这些语句都将会当作是请求变量(一个字符串或者是数字),不再会误以为是SQL语言本身。这样可以大大的减少SQL注入的机会。

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

AI Hentai Generator
Generate AI Hentai for free.

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

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:
