2010-12-16 56 views
4

関数GetSQLValueStringが表示されていますが、それが何を処理しているのか分かりません。あなた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ので自動的に生成された(そしておそらく他のアドビ製品?):私は、関数のパラメータとして$ CONNを追加することに注意してくださいフォームなど – chris

答えて

6

が存在する理由を理解するためにSQL injection問題を参照してください。この方法を使用する必要があります数値以外の値で、一重引用符で囲みます。この関数は、可変データをSQL照会に挿入するために作成されたものです。

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

アイデアをありがとう、私は今それを理解しています。しかし、この機能を使用しないと、どのような欠点がありますか? –

+0

@Charles:私の質問のSQLインジェクションリンクを参照してください。基本的にあなたが悪意のあるハッカーを使用しない場合、データベースからいくつかのデータを取得することができますいくつかのテーブル/行を削除し、多くの.... – RageZ

1

私の理解から、この関数は、MySQLにそれを渡すためにいくつかのデータをエスケープすると考えられます。関数はまた、ヌル値を処理し、必要に応じて引用符を付けます。

それは

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

は、この機能は、それがある場合はあなたの関数は、その後、MySQLのビルトインの文字列エスケープ機能を使用して文字列をエスケープ

0

この関数は、データ型固有の引用文字列を返します。これはSQLインジェクションを避けるために使用されます。

0

あなたの問題はmysqli_問題に関係していると思います。 mysql_mysqli_に変更し、データベースへの接続を最初のパラメータとして追加する必要があります。私の場合、データベースへの接続は$ conn_voteです。ログインのようなその既製のオブジェクトを挿入するとき

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

`