简体   繁体   English

备份数据库时是否需要使用addslashes()?

[英]Do I need to use addslashes() when backing up database?

I am using the code shown here , it uses addslashes() on the data fetched from the database before saving to file. 我正在使用此处显示的代码,它在保存到文件之前对从数据库中获取的数据使用addlashes()。

$row[$j] = addslashes($row[$j]);

My question is why and do I need to use this? 我的问题是为什么,我需要使用它吗? I thought you would do this when saving to the database not the other way round. 我以为您在保存到数据库时会这样做,而不是相反。 When I compare the results from the above script with the export from phpMyAdmin, the fields that contain serialized data are different. 当我将上述脚本的结果与phpMyAdmin的导出结果进行比较时,包含序列化数据的字段是不同的。 I would like to know if it would cause any problems when importing back into the database? 我想知道在重新导入数据库时​​是否会引起任何问题?

Script: 脚本:

'a:2:{i:0;s:5:\"Hello\";i:1;s:5:\"World\";}'

phpMyAdmin Export: phpMyAdmin导出:

'a:2:{i:0;s:5:"Hello";i:1;s:5:"World";}'

UPDATE 更新

All data is escaped when inserting into the database. 插入数据库时​​,所有数据均被转义。

Change from mysql to mysqli. 从mysql更改为mysqli。

SQL file outputs like: SQL文件输出如下:

INSERT INTO test (foo, bar) VALUES (1, '\'single quotes\'\r\n\"double quotes\"\r\n\\back slashes\\\r\n/forward slashes/\r\n');

SOLUTION

Used $mysqli->real_escape_string() and not addslashes() 使用$ mysqli-> real_escape_string()而不是addslashes()

inserting to db 插入数据库

When inserting data to a MySQL database you should be either using prepared statements or the proper escape function like mysql_real_escape_string . 在将数据插入MySQL数据库时,您应该使用预处理语句或适当的转义函数,例如mysql_real_escape_string addslashes has nothing to do with databases and should not be used. addslashes与数据库无关,不应使用。 Escaping is used as a general term but actually covers a large number of operations. 转义是一个通用术语,但实际上涵盖了许多操作。 Here it seems two uses of escaping are being talked about: 这里似乎在谈论转义的两种用途:

  1. Escaping dangerous values that could be inserted in to a database 转义可以插入数据库的危险值
  2. Escaping string quotes to avoid broken strings 转义字符串引号以避免损坏字符串

Most database escaping functions do a lot more than just escape quotes. 大多数数据库转义功能的作用不只是转义引号。 They escape illegal characters and well as invisible characters like \\0 ... this is because depending on the database you are using there are lots of ways of breaking an insert - not just by adding a closing quote. 它们转义了非法字符和\\ 0等不可见字符...这是因为根据所使用的数据库,有很多中断插入的方法-不仅仅是通过添加引号引起来。

Because someone seems to have missed my comment about mentioning PDO I will mention it again here. 因为有人似乎错过了我对提及PDO的评论,所以我将在这里再次提及。 It is far better to use PDO or some other database abstraction system along with prepared statments, this is because you no longer have to worry about escaping your values. 最好将PDO或其他一些数据库抽象系统与准备好的语句一起使用,这是因为您不再需要担心转义值。

outputting / dumping db values 输出/转储数据库值

In the mentioned backup your database script the original coder is using addslashes as a quick shorthand to make sure the outputted strings in the mysql dump are correctly formatted and wont break on re-insert. 在提到的数据库脚本备份中 ,原始编码人员使用addslashes作为快速速记,以确保mysql dump中输出的字符串格式正确,并且在重新插入时不会中断。 It has nothing to do with security. 它与安全性无关。

selecting values from a db 从数据库中选择值

Even if you escape your values on insert to the database, you will need to escape the quotes again when writing that data back in to any kind of export file that utilises strings. 即使您在插入数据库时​​转义了值,将数据写回到任何使用字符串的导出文件中时,也需要再次转义引号 This is only because you wish to protect your strings so that they are properly formatted. 这仅仅是因为您希望保护字符串,以便正确格式化它们。

When inserting escaped data into a database, the 'escape sequences' used will be converted back to their original values. 将转义的数据插入数据库时​​,使用的“转义序列”将转换回其原始值。 for example: 例如:

INSERT INTO table SET field = "my \"escaped\" value"

Once in the database the value will actually be: 一旦进入数据库,该值实际上将为:

my "escaped" value

So when you pull it back out of the database you will receive: 因此,当您将其拉回数据库时,您将收到:

my "escaped" value

So when you need to place this in a formatted string/dump, a dump that will be read back in by a parser, you will need to do some kind of escaping to format it correctly: 因此,当您需要将其放置在格式化的字符串/转储中时(解析器将读取转储中的内容),您将需要进行某种转义操作以正确格式化它:

$value_from_database = 'my "escaped" value';

echo '"' . $value_from_database . '"';

Will produce: 将产生:

"my "escaped" value"

Which will break any normal string parser, so you need to do something like: 这会破坏任何普通的字符串解析器,因此您需要执行以下操作:

$value_from_database = 'my "escaped" value';

echo '"' . addslashes($value_from_database) . '"';

To produce: 生产:

"my \"escaped\" value"

However, if it were me I'd just target the double quote and escape: 但是,如果是我,我只会将双引号和转义作为目标:

$value_from_database = 'my "escaped" value';

echo '"' . str_replace('"', '\\"', $value_from_database) . '"';

I think you are mixing two problems. 我认为您正在混淆两个问题。 The first problem is SQL Injection and to prevent this you would have to escape the data going into the database. 第一个问题是SQL注入 ,为防止这种情况,您必须对进入数据库的数据进行转义。 However by now there is a far more better way to do this. 但是,到目前为止,还有一种更好的方法。 Using prepared statements and bound parameters. 使用准备好的语句和绑定参数。 Example with PDO: PDO示例:

// setup a connection with the database
$dbConnection = new PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'pass');    
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// run query
$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = :name');    
$stmt->execute(array(':name' => $name));

// get data
foreach ($stmt as $row) {
    // do something with $row
}

The other thing you would have to worry about it XSS attacks which basically allows a possible attacker to inject code into your website. 您还需要担心的另一件事是XSS攻击 ,它基本上使可能的攻击者将代码注入您的网站。 To prevent this you should always use htmlspecialchars() when displaying data with possible information you cannot trust: 为防止这种情况,在显示包含您不信任的可能信息的数据时,应始终使用htmlspecialchars()

echo htmlspecialchars($dataFromUnsafeSource, ENT_QUOTES, 'UTF-8');

All data is escaped when inserting into the database. 插入数据库时​​,所有数据均被转义。

When using prepared statements and bound paramters this isn't needed anymore. 当使用准备好的语句和绑定参数时,就不再需要了。

Should I use addslashes() then use str_replace() to change \\" to "? 我应该使用addlashes()然后使用str_replace()将\\“更改为”吗?

addslashes() sounds like a crappy way to prevent anything. addslashes()听起来像是防止任何事情的cr脚方法。 So not needed AFAICT. 因此不需要AFAICT。

Another note about accessing the database and in the case you are still using the old mysql_* function: 关于访问数据库的另一条注释,并且在您仍然使用旧的mysql_*函数的情况下:

They are no longer maintained and the community has begun the deprecation process . 它们已不再维护,社区已开始弃用过程 See the red box ? 看到红色框了吗? Instead you should learn about prepared statements and use either PDO or MySQLi . 相反,您应该了解准备好的语句,并使用PDOMySQLi If you can't decide, this article will help to choose. 如果您不能决定, 本文将有助于选择。 If you care to learn, here is a good PDO tutorial . 如果您想学习, 这里有个不错的PDO教程

You should store data without modifying them. 您应该存储数据而不修改它们。

You should perform the needed escaping when outputting the data or putting them "inside" other data, like inside a database query. 在输出数据或将其“放入”其他数据时(例如在数据库查询内部),应执行所需的转义。

just use mysql_escape_string() instead of addslashes and ereg_replace as written in david walsh's blog. 只需使用mysql_escape_string()而不是大卫·沃尔什(David Walsh)博客中所写的addlashes和ereg_replace即可。

just try it it'll be better. 只是尝试它会更好。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM