使用mysql_real_escape_string()后返回回车

时间:2013-01-25 21:10:07

标签: sql mysql-real-escape-string carriage-return

我正在我的网站上完成并且想要使它非常安全sql注入。其中一个主要部分是在任何潜在的用户输入上使用mysql_real_escape_string()。

这对大多数事情都很好,我只需要使用'stripslashes'来返回要在页面上显示的原始内容。但是,我似乎无法获得回车。

有几个地方的用户可能会提交带有回车的片段(消息是一个例子,例如,'嗨约翰',然后两次返回主消息)。我怎样才能解决这个问题? Stripslashes只返回带有rnrn的消息,这是不行的。

作为一个例子(我尝试测试所有可能的输入)。

本文:

Once upon a time there was a guy named "steve" or 'john' or backslash (\)

Then, two lines later, he left.

在SQL数据库中保存这样:

Once upon a time there was a guy named \"steve\" or \'john\' or backslash (\\)\r\n\r\nThen, two lines later, he left.

使用stripslashes就像这样(请注意我正在执行以下操作将其打印到html - :

Once upon a time there was a guy named "steve" or 'john' or backslash (\)rnrnThen, two lines later, he left.

我已经尝试过nl2br,但由于某种原因它没有做任何事情。我非常喜欢这里的任何想法,非常感谢!

1 个答案:

答案 0 :(得分:1)

有很多事情搞砸了。

  1. mysql_real_escape_string()与sql注入和用户输入无关。
  2. 不应该是无条件的striplashes,而只是故意。
  3. 正确使用时,转义不会转到数据库。在取回时不需要条带。
  4. 如果您在数据库中有斜杠,则表示您有过多的转义。要么你的代码添加了两次斜杠,要么你正在使用占位符来逃避无用的
相关问题