我寫了下面的腳本作爲我有史以來第一個php mysql應用程序。我自學成才,代碼按預期工作。我的主機認爲它可能容易受到sql注入攻擊,但無法告訴我爲什麼或者應該改變什麼以使其更好。我敢肯定,它不是那麼幹淨,但如果任何人有任何建議或見解,我一定會很感激。PHP Sql注入漏洞
<form method="post" action="search.php?go" id="searchform">
<?php
$db=mysql_connect ("server", "*", "*") or die ('I cannot connect to the database because: ' . mysql_error());
$mydb=mysql_select_db("*");
$category_sql="SELECT distinct category FROM Members";
$category_Options="";
$category_result=mysql_query($category_sql) or die ('Error: '.mysql_error());
while ($row=mysql_fetch_array($category_result)) {
$category=$row["category"];
$category_Options.="<OPTION VALUE=\"$category\">".$category.'</option>';
}
?>
<p>
<SELECT NAME="category"><OPTION VALUE=0>Choose<?=$category_Options?></SELECT>
</p>
<input name="submit" "id="submit" type="submit" value="submit" />
</form>
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
$category=$_POST['category'];
$category=mysql_real_escape_string($category);
$sql="SELECT category, company, address, city, state, zip, phone, web, addescription, image
FROM Members
WHERE category LIKE '$category'";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$category2=$row["category"];
$company=$row["company"];
$address=$row["address"];
$city=$row["city"];
$state=$row["state"];
$zip=$row["zip"];
$phone=$row["phone"];
$web = $row["web"];
$addescription = $row["addescription"];
$image = $row["image"];
echo "<blockquote>";
if(@file_get_contents($image))
{
echo "<img src='".$image ."' class='image'/>\n";
}
else
{
}
echo "<p>\n";
echo "</br>".$category2 . "\n";
echo "</br><b>".$company . "</b>\n";
echo "</br>".$address . "\n";
echo "</br>".$city . ", ".$state. " ".$zip . "\n";
echo "</br>".$phone . "\n";
echo "</br><a href=http://".$web .">".$web ."</a>\n";
echo "</br>".$addescription . "\n";
echo "</br><a href=http://www.printfriendly.com style=color:#6D9F00;text-decoration:none; class=printfriendly onclick=window.print();return false; title=Printer Friendly and PDF><img style=border:none; src=http://cdn.printfriendly.com/pf-button.gif alt=Print Friendly and PDF/></a>\n";
echo "</p>";
echo "</blockquote>"
;
}
}
else{
echo "<p>Please select a Category</p>";
}
}
mysql_close($db)
?>
你是否檢查了這篇文章(http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php)?它幾乎涵蓋了它。 –
有很多文章來防止SQL注入。爲什麼不穀歌;) – tonoslfx
請注意,'mysql_xxx()'函數已被棄用,並被認爲已過時多年。如果你從使用它們的教程中學到了東西,那麼你幾乎肯定會做錯事情,因爲足夠老的推薦使用這些函數的教程也會足夠長,以至於不能再使用其他不好的做法。我建議找到一個網站有更多最新的教程來學習。 – Spudley