簡體   English   中英

mysqli連接不適用於mysqli_real_escape_string

[英]mysqli connection not working with mysqli_real_escape_string

我有一個簡單的演示php腳本正在嘗試。 我以前一直在使用不推薦使用的mysql_擴展,試圖從該版本遷移到mysqli_實在是過分的錯誤。 當試圖使用mysqli_real_escape_string php拋出a variable not defined error似乎無法弄清原因。

    <?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bridgoo";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection
if ($conn->connect_error) {
  //do stuff
  }
// prepare and bind
$stmt = $conn->prepare("INSERT INTO bridgoo_users (username, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $password, $email);


    //generate password hash using blowfish algorithm
   $password_hash =  password_hash($_POST['password'], PASSWORD_BCRYPT, ["cost" => 9]);

$username = clean_input($_POST['username']);
$password = clean_input($password_hash);
$email = clean_input($_POST['email']);


function clean_input($input){

    $input = htmlentities($input);
    $input = htmlspecialchars($input,ENT_QUOTES);
    $input = mysqli_real_escape_string($conn,$input);

    return $input;
}


$stmt->execute();
$stmt->close();
$conn->close();

?>

php錯誤通知:未定義變量:第24行的C:\\ xampp \\ server1 \\ htdocs \\ bridgoo \\ inc \\ register.php中的conn

關於undefined variable $conn的錯誤與PHP變量scope有關 ,其他人已經回答了。

但是您的代碼也需要其他一些建議。

如果使用查詢參數,則根本不需要清除輸入。 這就是使用查詢參數的要點:不需要mysqli_real_escape_string()因為參數是自動安全的。

實際上,您絕對不能使用mysqli_real_escape_string() ,因為您的數據將使用\\'轉義撇號按字面意義插入到數據庫中。

簡而言之: SQL參數和mysqli_real_escape_string()是互斥的。 不要兩者都做。

另外,即使使用mysqli_real_escape_string()而不是參數,也完全沒有必要使用htmlentities()htmlspecialchars ()mysqli_real_escape_string() SQL輸入。

這些與html相關的功能僅用於HTML輸出。 它們對於保護Web應用程序免受XSS漏洞非常重要,這是您需要了解的另一種無關的Web安全風險。 但是清理SQL輸入並不需要它們。

其他的建議:

  • 令人困惑的是,您在mysqli連接和應用程序數據中都重新使用了用戶名和密碼變量。 沒有理由重復使用變量,它們不會花費您任何費用。
  • 確保INSERT中參數的順序與傳遞給bind_param()的綁定變量的順序匹配。
  • 始終檢查prepare()execute()的返回值。 如果失敗,則返回FALSE。 如果失敗,則必須記錄該錯誤,並向用戶報告該頁面無效。

    我更喜歡將技術錯誤消息記錄到PHP錯誤日志文件中,並向用戶報告更友好的內容。

    養成在開發過程中保持窗口打開以查看PHP錯誤日志的習慣,這對您有很大幫助。

我建議您按照以下方式編寫代碼:

<?php

$mysql_servername = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_dbname = "bridgoo";

$conn = new mysqli($mysql_servername, $mysql_username, $mysql_password, $mysql_dbname);

if ($conn->connect_error) {
  error_log($conn->connect_error);
  die("Sorry, a database error occurred.");
}

$stmt = $conn->prepare("
  INSERT INTO bridgoo_users (username, password, email) 
  VALUES (?, ?, ?)");
if ($stmt === false) {
  error_log($conn->error);
  die("Sorry, a database error occurred.");
}

$stmt->bind_param("sss", $username, $password_hash, $email);

//generate password hash using blowfish algorithm
$password_hash = password_hash($_POST['password'], PASSWORD_BCRYPT, ["cost" => 9]);

$username = $_POST['username'];
$email = $_POST['email'];

if ($stmt->execute() === false) {
  error_log($conn->error);
  die("Sorry, a database error occurred.");
}

if ($stmt->affected_rows == 1) {
  echo "Success!"
} else {
  echo "Sorry, an unknown problem occurred, and your record was not saved."
}

您的clean_input函數無法訪問$conn變量,它位於其他作用域內

一種簡單的解決方案是將連接作為參數添加到函數中。 像這樣:

<?php
...


$username = clean_input($_POST['username'], $conn);
$password = clean_input($password_hash, $conn);
$email = clean_input($_POST['email'], $conn);


function clean_input($input, $conn){

...

最好的還是最干凈的解決方案,我將放在中間,但是它應該可以正常工作。

您應該將$conn作為參數傳遞給函數clean_input

$username = clean_input($conn, $_POST['username']);
$password = clean_input($conn, $password_hash);
$email = clean_input($conn, $_POST['email']);


function clean_input($conn, $input){

    $input = htmlentities($input);
    $input = htmlspecialchars($input,ENT_QUOTES);
    $input = mysqli_real_escape_string($conn,$input);

    return $input;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM