从表单/URL 清理 POST/GET 变量的最佳方法?
可能的重复:
在 PHP 中停止 SQL 注入的最佳方法
我正在创建一个使用 PHP 的网站,利用 MySQL 数据库并处理来自 URL 的表单和变量。 这些变量用于动态构造 SQL 查询字符串。 所以我需要一个强大的解决方案来确保没有人尝试 SQL 注入等。我的一个朋友说我实际上应该只使用存储过程来访问数据库,但这实际上并不可行,因为我使用的主机不不允许这些。
这是我正在使用的代码(它是包装数据库命令的类的一部分):
...
public function Sanitize($Variable)
{
if(is_resource($this->ServerConnection))
{
$Variable = str_replace(";", "", $Variable);
if(get_magic_quotes_gpc())
{
if(ini_get('magic_quotes_sybase'))
{
$Variable = str_replace("''", "'", $Variable);
}
else
{
$Variable = stripslashes($Variable);
}
}
return mysql_real_escape_string($Variable, $this->ServerConnection);
}
else
{
$this->PrintError("The Sanitize function is not available as there is no server connection.");
}
}
...
这个函数足够强大吗? 我应该做点别的吗?
Possible Duplicate:
Best way to stop SQL Injection in PHP
I am creating a website using PHP that makes use of a MySQL database and handles forms and variables from the URL. The variables are being using to dynamically construct SQL query strings. So i need a robust solution to make sure nobody is trying a SQL injection, etc.. A friend of mine has said that really i should only use stored procedures to access the database but that's not really feasible because the host i'm using doesn't allow these.
Here is the code i'm using (it's part of a class to wrap DB commands):
...
public function Sanitize($Variable)
{
if(is_resource($this->ServerConnection))
{
$Variable = str_replace(";", "", $Variable);
if(get_magic_quotes_gpc())
{
if(ini_get('magic_quotes_sybase'))
{
$Variable = str_replace("''", "'", $Variable);
}
else
{
$Variable = stripslashes($Variable);
}
}
return mysql_real_escape_string($Variable, $this->ServerConnection);
}
else
{
$this->PrintError("The Sanitize function is not available as there is no server connection.");
}
}
...
Is this function robust enough? Should i be doing anything else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能值得阅读这篇文章。
Might be worth reading this post.
没有最好的方法。 这取决于上下文。
认为数据好坏是一种有缺陷的思维模式。 数据只是数据。 它的使用环境决定了它是否是恶意的。 如果您在数据库服务器上直接执行某些单词,它们可能会很糟糕。 有些话如果给未成年人看是不好的。 这是关于上下文的。
There is no best way. It depends on the context.
It is a flawed mode of thinking that data are good or bad. Data is just data. It's the context in which it's used that makes it malicious or not. Some words may be bad if you execute them unadorned on a database server. Some words are bad if you display them to minors. It's about context.