2012-08-11 310 views
1

我對登錄腳本有幾個問題。這只是引導我到一個沒有錯誤的空白頁面。PHP登錄腳本問題

  • 如果我使用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(); 
    ?> 

回答

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

我沒有測試過它和我可能有一個錯字或兩個,但這是我的嘗試在改善你的腳本。讓我知道你的想法:

<?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(); 
?> 
+0

謝謝!我試了一下,但每次修改它都會給我錯誤頁面,即使登錄有效。任何想法爲什麼? – Undermine2k 2012-08-11 08:37:54

+0

你可以顯示登錄表的模式嗎?這可能有助於進一步解決問題。 – Katsuke 2012-08-12 01:40:37

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 

我認爲你可以繼續這樣做。 。 。

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()

0

對不起朋友,我不知道很多關於mysqli的。 但是,如果你願意,這可以用mysql輕鬆完成。

順便說一句,對於你的第三個問題, $result=mysqli_query($stmt);只返回資源ID的,如果有任何匹配的記錄爲您​​的搜索條件。並且mysqli_num_rows($result);將返回該條件有多少資源ID可用。用戶名和密碼將僅在mysqli_fetch_array($result);之後返回,這將使數據庫將記錄作爲這些資源ID的數組獲取。

希望你理解... :))