在 SQLite3 中避免 SQL 注入 [英] Avoiding SQL Injection in SQLite3
本文介绍了在 SQLite3 中避免 SQL 注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找出一种简单的方法来避免 SQL 注入,但到目前为止我只能提出两个想法:
I'm trying to figure out a good easy way to avoid SQL Injection and so far I've only been able to come up with two ideas:
- Base64 对用户输入进行编码(真的不想这样做)
- 使用正则表达式删除不需要的字符.(目前正在使用这个,不确定它是否 100% 安全)
这是我当前的代码:
<?php
$hash = $_GET['file'];
if (isset($hash))
{
$db = new SQLite3("Files.db");
if ($db != null)
{
$hash = preg_replace('/[^A-Za-z0-9 _.\-+=]/', '_', $hash);
if ($response = $db->query("SELECT [FILE] FROM '$hash'"))
{
echo $response->fetchArray()[0]; // File name is returned if successful.
}
}
}
?>
我的问题是我的做法是正确的还是有更好的方法?
My question is am I going about this the right way or are there any better ways to do this?
推荐答案
让你的数据库库为你做这件事.PHP Sqlite3 库支持准备语句:
Get your database library to do it for you. The PHP Sqlite3 library supports prepared statements:
$stmt = $db->prepare('SELECT bar FROM foo WHERE id=:id');
$stmt->bindValue(':id', 1, SQLITE3_INTEGER);
这篇关于在 SQLite3 中避免 SQL 注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文