1

ログイン スクリプトについていくつか質問があります。エラーのない空白のページに移動するだけです。

  • を使用している場合、クエリでorをmysqli使用する必要がありますか??$username$password
  • で何が起こっているのかわかりません$stmt -> fetch(); 私はそれを正しく使用していますか?
  • $result=mysqli_query($stmt);: この$result変数にはユーザー名とパスワードの両方が含まれていますか?
  • その場合、どのように機能しmysqli_num_rows($result)ますか?

  <?php 
    function clean($str)
       {
            $str = @trim($str);
            if(get_magic_quotes_gpc()) {
                $str = stripslashes($str);
            }
            return mysql_real_escape_string($str);
        }


        //Sanitize the POST values

    $username = clean($_POST['username']);
    $password = clean($_POST['password']);

    /* Create a new mysqli object with database connection parameters */
    $mysqli = mysqli_connect('localhost', 'root', '', 'draftdb');

    if(mysqli_connect_errno()) 
    {
          echo "Connection Failed: " . mysqli_connect_errno();
          exit();
    }
       /* Create a prepared statement */
    if($stmt = $mysqli -> prepare("SELECT Login_ID, Login_PW,
    FROM login  
    WHERE Login_ID='$username' AND Login_PW ='".md5($_POST['password'])."'"))
     {


          /* Bind parameters
             s - string, b - boolean, i - int, etc */
          $stmt -> bind_param("ss", $username, $password);

          /* Execute it */
          $stmt -> execute();

          /* Bind results */
          $stmt -> bind_result($username, $password);

          /* Fetch the value */

         while ($stmt->fetch()) 
         { 
              $result=mysqli_query($stmt);

        //Check whether the query was successful or not
        if($result)
         {//main if
            if(mysqli_num_rows($result) == 1) 
            {
                //Login Successful
                session_regenerate_id();
                $login = mysqli_fetch_assoc($result);
                $_SESSION['SESS_MEMBER_ID'] = $login['Login_ID'];
                //$_SESSION['SESS_FIRST_NAME'] = $login['firstname'];
                //$_SESSION['SESS_LAST_NAME'] = $login['lastname'];
                session_write_close();
                header("location: member-index.php");
                exit();
            }
            else {
                //Login failed
                header("location: login-failed.php");
                exit();
                 }
            }
            else 
            {
            die("Query failed");
            }


         }//main if close


          /* Close statement */
          $stmt -> close();
       }

       /* Close connection */
       $mysqli -> close();
    ?>
4

4 に答える 4

1

私はあなたの質問のそれぞれに対処しようとしていましたが、それらは非常に複雑で、それぞれに答えることはできませんでした. だから私はあなたの投稿されたスクリプトを自由に変更して、それが機能すると信じているものに変更しました. おそらく、まだいくつかの追加の調整が必要です。インラインで追加したコメントを確認してください。また、オブジェクト指向形式で mysqli 関数を使用する方法の詳細については、次の php ドキュメント ページを参照してください。

http://www.php.net/manual/en/mysqli-stmt.num-rows.php

http://www.php.net/manual/en/mysqli-stmt.execute.php

http://www.php.net/manual/en/mysqli-stmt.bind-result.php

http://www.php.net/manual/en/mysqli-stmt.bind-param.php

私はそれをテストしておらず、タイプミスが 1 つまたは 2 つある可能性がありますが、スクリプトを改善するための試みを次に示します。どう考えているか教えてください:

<?php 
function clean($str)
{
    $str = @trim($str);
    if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}

//Sanitize the POST values

$username = clean($_POST['username']);
$password = clean($_POST['password']);

/* Create a new mysqli object with database connection parameters */
$mysqli = mysqli_connect('localhost', 'root', '', 'draftdb');

if(mysqli_connect_errno()) 
{
    echo "Connection Failed: " . mysqli_connect_errno();
    exit();
}

/* Is your username the same as the login_id? If not you need to change this query's where to use the username column not the login_id. */

/* Create a prepared statement */
if($stmt = $mysqli -> prepare("
    SELECT Login_ID, firstname, lastname
    FROM login  
    WHERE Login_ID=? AND Login_PW=?
"))
{
    /* Bind parameters
         s - string, b - boolean, i - int, etc */
    $stmt -> bind_param("ss", $username, md5($password));

    /* Execute it */
    $result = $stmt -> execute();

    //Check whether the query was successful or not
    if ($result === false) {
        die("Query failed");
    }

    /* Bind results to variables that will be used within the fetch() loop. */
    $stmt -> bind_result($login_id, $firstname, $lastname);

    /* Check the number of rows returned. */
    if ($stmt->num_rows !== 1) {
        //Login failed
        header("location: login-failed.php");
        exit();
    }

    /* Iterate over the results of the query. */
    while ($stmt->fetch()) 
    { 
        //Login Successful
        session_regenerate_id();
        /* We can use $login_id, $firstname and $lastname cause we binded the result to those variables above. */
        $_SESSION['SESS_MEMBER_ID'] = $login_id;
        //$_SESSION['SESS_FIRST_NAME'] = $firstname;
        //$_SESSION['SESS_LAST_NAME'] = $lastname;
        session_write_close();
        header("location: member-index.php");
        exit();
     }//main if close

      /* Close statement */
      $stmt -> close();
   }

   /* Close connection */
   $mysqli -> close();
?>
于 2012-08-11T06:42:34.107 に答える
1
  1. 準備されたステートメントを使用している場合、通常、変数をステートメントに代入するべきではありません。あなたが置く ?そこにプレース$stmt->bind_param()ホルダーを配置し、これらのプレースホルダーを変数に関連付けるために使用します。

  2. を使用した後$stmt->fetch()、バインドした変数を参照して$stmt->bind_result、SELECT の結果にアクセスできます。

  3. mysqli_query準備済みステートメントを使用している場合は、まったく使用しないでください。それがどのように機能するかについての質問に答えるため$resultに、実際のデータは含まれていません。$row = mysqli_fetch_assoc($result)ユーザー名とパスワードを $row に取得するようなものを呼び出します。

  4. 使用する必要があります$stmt->num_rows()

于 2012-08-11T06:22:31.800 に答える
0

問題はコードのこの部分にあると思います

 while ($stmt->fetch()) 
     { 
          $result=mysqli_query($stmt);

ステートメントを実行し、フェッチしました。再度照会する必要はありません。. . . PDO を使用しているため、mysqli には詳しくありませんが、結果を $username、$password にバインドしたので、これらのバインドされた変数を使用して戻り値にアクセスできると思います。

while ($result = $stmt->fetch()) 
 { 
if($result->num_rows == 1) 
        {
$_SESSION['SESS_MEMBER_ID'] = $result['LOGIN_ID']
//////or $_SESSION['SESS_MEMBER_ID'] = $username  

このように進めることができると思います。. .

于 2012-08-11T06:16:44.857 に答える
0

申し訳ありませんが、mysqli についてあまり知りません。しかし、これは必要に応じて mysql で簡単に行うことができます。

ちなみに、3 番目の質問で $result=mysqli_query($stmt);は、検索条件に一致するレコードがある場合、リソース ID のみが返されます。そしてmysqli_num_rows($result);、その基準で利用可能なリソース ID の数を返します。ユーザー名とパスワードはmysqli_fetch_array($result);、データベースがそれらのリソース ID の配列としてレコードを取得するようにした後にのみ返されます。

ご理解いただければ幸いです... :))

于 2012-08-11T06:26:12.217 に答える