【发布时间】:2012-10-24 20:13:28
【问题描述】:
我创建了一个网站,我只显示我的数据库表中的项目,我将变量从一个页面传递到另一个页面以显示某些项目,没有添加、删除或编辑我的表项目在我的网站上只是显示信息。
$aaa = _POST['aaa'];
$databasehost = "localhost";
$databasename = "mydb";
$databaseusername = "user";
$databasepassword = "password";
// Connect to the database server
$dbcnx = @mysql_connect($databasehost, $databaseusername, $databasepassword);
if (!$dbcnx) {
echo( "<font color='red'><P>can't connect to server.</P></font>" );
exit();
}
// Select the database
if (! @mysql_select_db($databasename) ) {
echo( "<font color='red'><P>can't connect to db </P></font>");
exit();
}
$aaa = mysql_real_escape_string($aaa)
// and with $aaa I do my query
我已经阅读到使用 mysql_real_escape_string() 保护我的变量我停止了对我的查询的任何注入,但我觉得很容易受到以下影响:
$databasehost = "localhost";
$databasename = "mydb";
$databaseusername = "user";
$databasepassword = "password";
我只是偏执,还是有办法保护这些连接到服务器和数据库的信息?
【问题讨论】:
-
请不要使用
mysql_*函数编写新代码。它们不再维护,社区已经开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDO 或MySQLi。如果你不能决定哪一个,this article 会帮助你。如果你选择 PDO,here is good tutorial。另见Why shouldn't I use mysql functions in PHP? -
以上是预设回复,但我相信它适用于您的问题。
-
这条线
$aaa = _POST['aaa']正在保护任何人不使用您的网站。 -
为什么我使用 mysql_* 会得到负票?感谢您的更正以及向我展示了正确的方式,但我认为这不值得一票!
-
@Daedalus 谢谢你,我会调查的。