2012-01-05 51 views
1

这是我当前的代码我的代码安全的,因为它代表是从SQL注入

$search=$_GET["Search"]; 
$search = addcslashes(mysql_real_escape_string($search), '%_'); 

此代码生成由Dreamweaver CS4从这里开始

if (!function_exists("GetSQLValueString")) { 
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    { 
     if (PHP_VERSION < 6) { 
     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
     } 

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

    switch ($theType) { 
    case "text": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break;  
    case "long": 
    case "int": 
     $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
     break; 
    case "double": 
     $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
     break; 
    case "date": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break; 
    case "defined": 
     $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
     break; 
    } 
    return $theValue; 
} 
} 

mysql_select_db($database_Echos, $Echos); 

SQL的语句是Querys分贝

$query_Recordset1 = "SELECT catelogue.ARTIST, catelogue.TITLE, catelogue.`CAT NO.`, catelogue.FORMAT, catelogue.`IMAGE PATH` FROM catelogue WHERE catelogue.TITLE LIKE '%$search%'"; 

更多Dremweaver代码

$Recordset1 = mysql_query($query_Recordset1, $Echos) or die(mysql_error()); 
    $row_Recordset1 = mysql_fetch_assoc($Recordset1); 
    $totalRows_Recordset1 = mysql_num_rows($Recordset1); 

在这之后得到的数据显示在表格格式

我还是新的这一切,并在最近几天已经发现了,你可以把staements到我的搜索词,破坏8个月数据库工作。这不是像数据库没有备份,但我宁愿安全,然后抱歉。

我已阅读准备好的声明是阻止这种How can I prevent SQL injection in PHP?的最佳方法。

但正如我所说我对此很陌生,对此很少理解。

那么简单的放是我的代码,DW创建足够安全

是否有事情,应该改变,使其更简单

而且如果它不够安全我如何使它更安全吗?

我在这里使用准备的书吗?如果是的话可以有人请解释如何在这里使用它

请任何帮助,将不胜感激

回答

6

你的代码是从大量的不良做法的痛苦,但它也不是完全不安全或完全糟糕的通常我习惯看到的PHP代码段。

简而言之 - 你不想使用PHP的mysql_函数。你应该做的是使用PDO

mysql_real_escape_string是安全的,但不是100%。有关更好的解释,请参阅解释when mysql_real_escape_string is vulnerable的链接。

因此,您要做的就是用PDO创建预准备语句,然后绑定输入值。 PDO根据所使用的字符集和数据库清理它们,这使得它100%安全,不用担心是否错过了某些东西。

+0

因此,简单来说,一个准备好的语句会查看你的数据库中的所有字符,然后只允许这些字符。 – Mark 2012-01-05 12:20:13

+0

准备好的语句是一个查询,你发送到数据库,它被解析和“预编译”只将参数发送到数据库服务器,并知道如何在查询中使用它们 - 因此,它解析一次,使用多次不同的值。因此,PDO会做什么是检查正在使用的数据库驱动程序(MySQL,PostgreSQL,MSSQL等)以及什么字符集(UTF8或其他)以及它将根据获取的信息知道如何处理某些特殊字符。因此,它会正确地清理这些特殊字符,并且它们会“安全”使用。 – 2012-01-05 12:31:17

+0

有了PDO,我必须安装一些东西才能使代码正常工作,或者它已经完成了wamp,我只需更改我的代码 – Mark 2012-01-05 12:58:26