在响应previous question I had posted,还有就是我被我的数据库对外开放SQL注入的响应。我应该采取哪些措施来防止SQL注入?
<?php
$firstname = stripslashes(strip_tags($_POST['firstname']));
$lastname = stripslashes(strip_tags($_POST['lastname']));
$email = stripslashes(strip_tags($_POST['email']));
$title = stripslashes(strip_tags($_POST['title']));
$organization = stripslashes(strip_tags($_POST['organization']));
$pdbHost = "localhost";
$pdbUserName = "******";
$pdbPassword = "******";
$pdbName = "db1080824_emails";
// Connect to mySQL
$conlink = mysql_connect($pdbHost, $pdbUserName, $pdbPassword);
if(!$conlink) {die('Unable to connect to '.$pdbHost);}
if (!mysql_select_db($pdbName, $conlink)){die('Cannot find database '.$pdbName);}
//SQL query
$SQL2="INSERT INTO `db1080824_emails`.`emails` (`record_id` ,`firstname`,`lastname`,`email`,`title`,`organization`)VALUES (NULL , '".$firstname."', '".$lastname."', '".$email."', '".$title."', '".$organization."')";
mysql_query($SQL2);
// Connect to Closing the connection
mysql_close($conlink);
?>
一个建议是,我可以做服务器端验证检查“/^[A-ZA-Z0-9] /”,以确保没有SQL注入可能发生,但有足够的:下面的代码是给还是应该遵循以确保数据卫生的最佳做法?
http://en.wikipedia.org/wiki/SQL_injection - 阅读更多关于它是如何做和浩w“撤销” – Joseph 2012-03-04 07:55:51
只使用以前准备好的语句。期。 – knittl 2012-03-04 07:59:06
第一步:**停止使用mysql_query **并使用像PDO或mysqli这样的现代API。两者都支持预准备语句 - 如果使用得当,可以大大减少SQL注入的时间。 – cHao 2012-03-04 08:00:21