php 使帖子安全

发布于 2024-11-29 11:21:10 字数 644 浏览 2 评论 0原文

我在完成最简单的任务时遇到了困难。 html 页面有一个 textarea 标记,它使用 $.get() 和 jquery 将数据发送到 php 文件。

从那里我使用了从 htmlspecialchars 到 addslashes 的各种方法。 但似乎没有什么能按照应有的方式工作,要么因为留下了一个 \ 而抛出错误,要么不保留换行符。

如何将文本区域的值传递给 php 文件,使其具有注入证明和 json 兼容。 这样当我使用 mysql 访问它并使用 $.formatJSON() 对其进行格式化时。它可以正常工作并保留所有新行字符吗?

//php

$result = htmlspecialchars($_GET["message"]);

mysql_query("INSERT INTO table (message) VALUES ($result)");


//jquery
$.ajax({
 url:("http://websitename.com/post.php?message=" + message + "&callback=?"),
 dataType: 'JSONP',
 success:function(json){
    callback($.parseJSON(json));
 },
 error:function(){
 },
});

I am having trouble with the simplest of tasks.
The html page has a single textarea tag that sends the data to a php file using $.get() with jquery

From there I use a variety of methods from htmlspecialchars to addslashes.
But nothing seems to work the way it should, either throwing off an error because a single \ was left behind or not preserving newlines.

How can I pass a textarea's value to a php file, make it injection proof and json compatible.
So that when I access it with mysql and format it using $.formatJSON(). It works without any issues and preserves all the new line characters?

//php

$result = htmlspecialchars($_GET["message"]);

mysql_query("INSERT INTO table (message) VALUES ($result)");


//jquery
$.ajax({
 url:("http://websitename.com/post.php?message=" + message + "&callback=?"),
 dataType: 'JSONP',
 success:function(json){
    callback($.parseJSON(json));
 },
 error:function(){
 },
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○愚か者の日 2024-12-06 11:21:10

现在推荐的做法是使用准备好的语句。这样 SQL 注入就不存在了。如果在分析代码后发现这太慢,您应该求助于使用 mysql_(real_)escape_string。

作为安全的另一步骤,您可以安装入侵检测系统(例如 PHPIDS)来添加另一个安全层。

不要使用addslashes 或类似的东西,它们并不是为了转义SQL 查询的用户输入。

另外,在您的情况下,您不应该通过 GET 传递文本区域内容,而应该通过 POST 传递,如 jQuery 文档< /a>.此外,dataType 应该是“json”,之后您不需要解析它。 此处给出了在 jQuery 中从 PHP 获取 JSON 的示例。

The recommended practice nowadays is to use prepared statements. This way SQL injections are out of the question. If this proves to be too slow after profiling the code, you should resort to using mysql_(real_)escape_string.

As another step of security you can install an Intrusion Detection System like PHPIDS to add another security layer.

Don't use addslashes or similar, they are not meant for escaping user input for SQL queries.

Also in your case you should not pass the textarea contents via GET but rather via POST as described in the jQuery documentation. Furthermore, the dataType should be 'json' and you don't need to parse it afterwards. An example for getting JSON from PHP in jQuery is given here.

小鸟爱天空丶 2024-12-06 11:21:10

在 MySQL 查询中使用值之前,请使用 mysql_real_escape_string。此外,您应该在 ($result) 中的 $result 两边加上单引号。

Before using a value in a MySQL query, use mysql_real_escape_string. In addition, you should put single quotes around $result in ($result).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文