2017-06-18 4 views
0

SQLインジェクションから自分のウェブサイトを保護しようとしています。だから私は自分のコードを変更し、それをプリペアドステートメントに置き換えることにしました。私は以下のコードで軽度のミスステイクをしたと思います。接続データベース中の準備文

<?php 
session_start(); 


$host= 'localhost'; 
$user='root'; 
$pass=''; 
$db='gameforum'; 


$conn= mysqli_connect($host, $user, $pass, $db); 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
} 

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

    if ($password!==$rpassword) { 
$_SESSION['err']="Passwords did not match, please try again!"; 
header("Location: index.php"); 
    $conn->close(); 
} 
else { 
    $stmt = $conn->prepare("INSERT INTO users (username, password, rpassword, email) VALUES (?, ?, ?, ?)"); 
    if(!$stmt){ 
      echo "false"; 
    }else { 
    $stmt->bind_param("ssss", $username, $password, $rpassword, $email); 
     if ($stmt->execute === TRUE) { 
     $redirectUrl = 'index.php'; 

    $_SESSION['registrationsuccessful']="Your account was successfully created! You may now log in to your account."; 
    header("Location: index.php"); 
}else{ 
    $_SESSION['alreadyexists']="Username or email already exists!"; 
header("Location: index.php"); 
    $stmt->close(); 
    $conn->close(); 
    } 
$stmt->close(); 
$conn->close(); 
} 
    } 

私が今直面しています問題は、私は実際には存在しないアカウントを作成しようとすると、私はメッセージ「ユーザーがすでに存在」してしまうことがあります。ありがとう!

+0

'$ stmt-> execute()'を2回呼び出すのではなく、変数に代入して真偽をテストしてください。 – RamRaider

答えて

0

すでにexecuteステートメントが実行されています。それらのうちの1つを取り外します。あるいは、実行文の1つのみで成功を確認してください。

+0

Ok上記のコードを更新しました。私は空白のページに "false"を付けました。これは、この$ stmt = $ conn-> prepareが今失敗することを意味します –

0

問題の理由は$stmt->execute()の2番目の使用でしたが、コードにいくつかの変更を加えることができました。

db接続を作成する最初のロジックif ($password!==$rpassword)が成功した場合〜はそうでなければ意味がないようです。私はこれより3ではなく1つのセッション変数を使用します。おそらく後で他のページの値をチェックするのが簡単になります。

最初の$stmt->execute()の結果を変数に代入し、必要に応じてさらに論理テストでその変数を使用します。

エラーメッセージについては、開発のために冗長なエラーメッセージを表示することはできますが、実際には使用しないでください。したがって、削除されたメッセージは$conn->connect_errorです。 proceduralobject orientatedコードの混合

もうひとつは、おそらく良い習慣と見なされていません - より良いどちらか一方(OOが容易になり、私は思う)

<?php 
    session_start(); 

    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $rpassword = $_POST['rpassword']; 
    $email = $_POST['email']; 
    $_SESSION['registration_status']=""; 

    if ($password!==$rpassword) { 
     $_SESSION['registration_status']="Passwords did not match, please try again!"; 
     exit(header("Location: index.php")); 

    } else { 

     $host= 'localhost'; 
     $user='root'; 
     $pass=''; 
     $db='gameforum'; 


     $conn= mysqli_connect($host, $user, $pass, $db); 
     if($conn->connect_error) die("Connection failed"); 


     $stmt = $conn->prepare("INSERT INTO users (`username`, `password`, `rpassword`, `email`) VALUES (?, ?, ?, ?)"); 
     if($stmt){ 

      $stmt->bind_param("ssss", $username, $password, $rpassword, $email); 
      $result = $stmt->execute(); 

      /* use the return value from stmt->execute() */ 
      $_SESSION['registration_status'] = $result ? "Your account was successfully created! You may now log in to your account." : "Username or email already exists!"; 

      $stmt->close(); 
     } 

     $conn->close(); 

     exit(header("Location: index.php")); 
     } 
    } 
?> 
0

あなたはこれを試すことができ、に固執する

<?php 
// if session not start, start now 
!session_id() ? session_start() : null; 
$mysql_server = "localhost"; 
$mysql_user = "root"; 
$mysql_password = ""; 
$mysql_db = "gameforum"; 
// connect db connection 
$conn = new mysqli($mysql_server, $mysql_user, $mysql_password, $mysql_db); 
// chck if connection has error 
if ($conn->connect_errno) { 
    printf("Connection failed: %s \n", $conn->connect_error); 
    exit(); 
} 
// db encoding 
$conn->set_charset("utf8"); 
// when POST happen 
if (isset($_POST) && !empty($_POST)) { 
    // convert POST array key as PHP variable 
    extract($_POST); 
    // if password matched with confirm password 
    if ($password === $rpassword) { 
     // create insert query with prepare 
     $stmt = $conn->prepare("INSERT INTO users (username, password, rpassword, email) VALUES (?, ?, ?, ?)"); 
     // if prepare fine, there is no query or mysql error 
     if ($stmt) { 
      // bind real values 
      $stmt->bind_param("ssss", $username, $password, $rpassword, $email); 
      // if query executed 
      if ($stmt->execute()) { 
       // success message & redirect 
       $_SESSION['registrationsuccessful'] = "Your account was successfully created! You may now log in to your account."; 
       header("Location: index.php"); 
       exit(); 
      } else { 
       // query error & redirect 
       $_SESSION['alreadyexists'] = "There was an error or Username/Email already exists!"; 
       header("Location: index.php"); 
       exit(); 
      } 
     } 
    } else { 
     // password matched failed 
     $_SESSION['err'] = "Passwords did not match, please try again!"; 
     header("Location: index.php"); 
     exit(); 
    } 
} 

私は接続をクローズしていないため、PHPはスクリプトの最後に開いているすべてのファイルと接続を閉じます。