使用“使用PHP安全方式将数据输入到mysql”插入数据时出现的问题
我正在尝试使用表单将数据输入MySQL,并且还使用mysql_real_escape_string
来实现此目的。不幸的是我的输出有问题。它显示 \
s,或者如果我使用 stripslashes
那么它会删除所有斜杠。
如果我使用反斜杠 \ 提交 web 表单
,我会得到以下输出:
"web\'s forms using backslash \\"
See I got a double backslash.但是,如果我使用 stripslashes
函数,那么它会删除所有斜杠,但也会删除输入的斜杠,并且输出为
"web's forms using backslash"
Here,不显示反斜杠,但末尾应该有一个反斜杠。
问题是,如果有人在密码字段和任何其他字段中使用反斜杠,那么反斜杠将被删除或显示两次。还请告诉我什么是显示输出 htmlentities 或 htmlspecialchars 的最佳选择
I am trying to input data using forms into the MySQL, and also using mysql_real_escape_string
for this purpose. Unfortunately I am having a problem with the output. It displays \
s, or if I use stripslashes
then it removes all slashes.
If I submit web's forms using backslash \
I get this output:
"web\'s forms using backslash \\"
See I got a double backslash. But if I use the stripslashes
function then it removes all slashes but also removes inputed slash and the output is
"web's forms using backslash"
Here, no backslash is displayed, but there should be one backslash at the end.
The problem is that if someone uses backslash in password field and any other filed, then the backslash will be stripped or displayed twice.And also please tell me what is best for displaying output htmlentities or htmlspecialchars
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已启用魔术引号。您需要完全禁用它们,因为它们在安全性方面不好。
从 php.ini 将它们设置为
off
(首选):或者您可以在运行时禁用它们:
You have magic quotes turned on. You need to disable them altogether as they are not good in terms of security.
Set them to
off
from php.ini (preferred):Or you can disable them at runtime:
使用 mysqli 库和准备好的语句。然后所有字符都作为数据输入,您不需要弄乱所有这些东西。
哪些字符不会使用 mysqli 准备好的语句进行转义?< /a>
为什么使用 mysql 准备好的语句比使用常见的转义函数更安全吗?
Use the mysqli library and Prepared Statements. Then all characters go in as data and you don't need to mess with all this stuff.
What characters are NOT escaped with a mysqli prepared statement?
Why is using a mysql prepared statement more secure than using the common escape functions?
向mysql输入数据的安全方法
您应该使用 PDO(新改进的方法)预准备语句,它比前辈(
mysql_real_escape_string 等
)更安全、更快。 为什么你应该这样做使用 PHP 的 PDO 进行数据库访问 更深入的细节。显示输出
新的改进方法是使用过滤器。我特别建议您阅读 PHP 创建者 Rasmus Ledorf 的所有这些性能和安全性幻灯片。 http://talks.php.net/ 有很多不错的幻灯片,您应该看看在。
Safe way to input data to mysql
You should use PDO(new improved way) prepared statement which are safer and faster then there predecessors(
mysql_real_escape_string, etc
). Why you Should be using PHP’s PDO for Database Access goes into deeper details.Displaying output
The new and improved way is to use filter. In particular I would advise you to read all these Performance and Security slides from PHP creator Rasmus Ledorf. http://talks.php.net/ has a lot of good slides in general which you should have a look at.