1

我的登录脚本有几个问题。它只是将我引导到一个没有错误的空白页面。

  • 如果我使用mysqli,是否需要在查询中使用?or$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
  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 回答
1

我试图解决你的每一个问题,但是它们变得如此复杂,以至于我不能只给你一个答案。所以我冒昧地修改了你发布的脚本,我相信它会让它工作。也许仍然需要一些额外的调整。请查看我添加的内联评论。此外,查看以下 php 文档页面以获取有关以面向对象形式使用 mysqli 函数的更多信息:

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

我还没有测试过,我可能有一两个错字,但这是我改进脚本的尝试。让我知道你的想法:

<?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 回答
0

我认为,问题在于您的这部分代码

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

您已经执行了该语句,并获取了它;无需您再次查询。. . . 我不熟悉 mysqli,因为我使用 PDO,但我认为由于您将结果绑定到 $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 轻松完成。

顺便说一句,对于您的第三个问题, $result=mysqli_query($stmt);如果您的搜索条件有任何匹配记录,则仅返回资源 ID。并将mysqli_num_rows($result);返回有多少资源 ID 可用于该条件。用户名和密码只会在此之后返回,mysqli_fetch_array($result);这将使数据库将记录作为这些资源 ID 的数组获取。

希望你能理解...:))

于 2012-08-11T06:26:12.217 回答