在 CakePHP 中保存数据时使用 mysql_real_escape_string 和 htmlspecialchars 的最佳方法是什么?
我将 FCKEditor 与 CakePHP 一起使用,当我保存从编辑器发送的数据时,我想在数据上运行 htmlspecialchars() 和 mysql_real_escape_string() 函数来清理它,然后再将其存储到数据库中。 问题是我不太确定在 CakePHP 框架中的哪里执行此操作。 我在控制器中尝试过这样的操作:
function add()
{
if (!empty($this->data))
{
if ($this->Article->save(mysql_real_escape_string(htmlspecialchars($this->data))))
{
$this->Session->setFlash('Your article has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
但是 $this->data 是一个数组,并且这些函数需要字符串,所以这不起作用。 我是否在模型的验证数组中执行此操作? 我不知道。 另外,请告诉我在 mysql_real_escape_string() 内部运行 htmlspecialchars() 是否不是一个好的做法。
I am using FCKEditor with CakePHP and when I save data sent from the editor I want to run the htmlspecialchars() and mysql_real_escape_string() functions on the data to clean it before I store it in my database. The problem is I am not really sure where to do this within the CakePHP framework. I tried in the controller like this:
function add()
{
if (!empty($this->data))
{
if ($this->Article->save(mysql_real_escape_string(htmlspecialchars($this->data))))
{
$this->Session->setFlash('Your article has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
However $this->data is an array and those functions expect strings so that won't work. Do I do it in the validate array of the model? I have no idea. Also, let me know if running htmlspecialchars() inside of mysql_real_escape_string() is not a good practice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在保存数据时不要使用
htmlspecialchars()
,而在将数据输出到HTML时使用它。 如果您需要在 HTML 之外的某些上下文中查看数据怎么办?另外,我不是 Cake 用户,但如果您在保存数据时需要应用 mysql_real_escape_string() ,我会感到惊讶。 数据库访问层应该保护您免受 SQL 注入,并且通过手动执行,您最终将存储双重转义的字符串。
Don't use
htmlspecialchars()
when you save data, use it when you output data to HTML. What if you need to look at the data in some context other than HTML?Also I'm not a Cake user, but I'd be surprised if you need to apply
mysql_real_escape_string()
as you save data either. The database access layer should protect you against SQL injection, and by doing it manually you're going to end up storing doubly-escaped strings.简短而简单的答案是 - 如果数据库访问已被抽象掉,则根本不需要调用这些函数。
唯一需要它们的地方是如果您从字符串位构建实际的 SQL。 无论如何你都不应该这样做,但那是另一个故事了。
底线是——框架会做正确的事情,不要干涉。
编辑:正如 Bill Karwin 指出的 -
htmlspecialchars()
来自完全错误的部门。The short and simple answer is - if the database access has been abstracted away, there is no need for you to call these functions at all.
The only place where they are needed is if you build actual SQL from bits of strings. Which you should not do anyway, but that's another story.
Bottom line is - the framework will do the right thing, don't interfere.
EDIT: As Bill Karwin points out -
htmlspecialchars()
is from the completely wrong department here.如果您使用 CakePHP 构建自己的 SQL 字符串,则 CakePHP 提供转义函数:
http://book .cakephp.org/view/1186/escape
If you are building your own SQL strings with CakePHP, then CakePHP provides the escape function:
http://book.cakephp.org/view/1186/escape