2010-12-16 138 views
4

我看到一個函數Ge​​tSQLValueString,我不知道它在處理什麼,有人可以給我一些想法嗎?
感謝您PHP - GetSQLValueString函數

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{ 
    $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; 
} 

功能這裏使用:

if (isset($_POST['username'])) { 
    $loginUsername=$_POST['username']; 
    $password=$_POST['password']; 
    $MM_fldUserAuthorization = ""; 
    $MM_redirectLoginSuccess = "main.php"; 
    $MM_redirectLoginFailed = "login_form.php"; 
    $MM_redirecttoReferrer = false; 
    mysql_select_db($database_connection1, $connection1); 

    $LoginRS__query=sprintf("SELECT username, password FROM member WHERE username=%s AND password=%s", 
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
... 
+3

只是爲了信息,通過Dreamweaver會自動生成該功能將像登錄其現成的對象時表格等 – chris 2012-06-23 09:33:48

回答

6

你的函數轉義使用MySQL內置的字符串避開功能,那麼如果它是一個字符串非數字值,用單引號括起來。這個函數是爲了將變量數據插入SQL查詢而編寫的。

$sql = "SELECT * FROM users WHERE username = " . GetSQLValueString($_GET['username'], 'text'); 
$result = mysql_query($sql); 
+0

感謝您的想法,我現在明白了。但是如果我們不使用這個功能,缺點是什麼? – 2010-12-16 07:01:37

+0

@Charles:請參閱我的問題上的SQL注入鏈接。基本上如果你不使用一些邪惡的黑客可以從你的數據庫中獲取一些數據刪除一些表/行和更多.... – RageZ 2010-12-16 07:17:39

1

從我的理解這個功能可能是逃避一些數據,將它傳遞到MySQL。該函數還處理空值並根據需要放置一些引號。

應該用這種方式

GetSQLValueString("a value that I want to escape's", 'text'); 

看明白SQL injection問題爲什麼這個功能存在

0

該函數返回數據類型特定的帶引號的字符串。這用於避免sql注入。

0

我想你的問題與mysqli_問題有關。您需要將所有mysql_更改爲mysqli_並將連接添加到數據庫作爲第一個參數。在我的情況下,連接到數據庫是$ conn_vote。請注意,我說$康恩作爲函數的參數:(?也許其他Adobe產品)

function GetSQLValueString($conn_vote, $theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    { 
     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 

     $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conn_vote, $theValue) : mysqli_escape_string($conn_vote, $theValue);`enter code here` 

     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; 
    } 
    } 

`