2012-07-13 31 views
-1

我找不到我的錯誤,我可以幫忙嗎?我正在嘗試使用cookie登錄代碼。先謝謝你!我似乎不能看到我的錯誤。我希望有人能看到我缺少的東西。這個錯誤是在第47行的某個地方,但我知道這並不意味着它就是這樣。我的代碼中意外的T_ELSE錯誤,有人能看到嗎?

<?php 
    if(isset($_POST['sent']) && $_POST['sent'] == "yes") 
    { 
     foreach($_POST as $field => $value) 
     { 
      if($value == "") 
      { 
       $blank_array[$field]= $value; 
      } 
      else 
      { 
       $good_data[$field]=strip_tags(trim($value)); 
      } 
     } 
    } 

    if(@sizeof($blank_array) > 0) 
    { 
     $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'> Error.</p>"; 
     extract($blank_array); 
     extract($good_data); 
     include("form_log.php"); 
     exit(); 
    } 

    include("dbstuff.php"); 
    $cxn = mysqli_connect($host,$user,$password,$database) or die ("coulnt connect"); 
    $query = "SELECT first_name FROM customer WHERE user_name='$_POST[user_name]' AND  password=md5('$_POST[password]')"; 
    $result = mysqli_query($cxn,$query) or die ("couldnt query"); 
    $n_row = mysqli_num_rows($result); 

    if($n_row < 1) 
    { 
     $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'> Not found.  </p>"; 
     extract($_POST); 
     include("form_log.php"); 
     exit(); 
    } 
    else 
    { 
     $row=mysqli_fetch_assoc($result); 
     setcookie("first_name",$row['first_name']); 
     setcookie("auth","yes"); 
     header("Location: secret_page_cookie.php"); 
    } 

    else 
    { 
     $user_name = ""; 
     $password = ""; 
     include("form_log.php"); 
    } 
?> 

對不起沒有縮進,但這很難縮進。第二別的去,如果(@sizeof)..

+7

幾乎所有開發人員都使用縮進來格式化他們的代碼是有原因的。 – 2012-07-13 17:40:55

+1

我有義務在這裏提到SQL注入攻擊和弱密碼哈希。 – Ryan 2012-07-13 17:43:01

+1

另外,像你這樣使用'include'是你很快就會後悔的事情,在沒有打開form_log.php文件的情況下實際閱讀這段代碼中的內容是完全不可能的。看起來像一個理想的候選人轉換成我的功能! – fvu 2012-07-13 17:44:02

回答

7
if ($n_row < 1) { 
    $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'> Not found.  </p>"; 
    extract($_POST); 
    include("form_log.php"); 
    exit(); 
} else { 
    $row=mysqli_fetch_assoc($result); 
    setcookie("first_name",$row['first_name']); 
    setcookie("auth","yes"); 
    header("Location: secret_page_cookie.php"); 
} else { 
    $user_name = ""; 
    $password = ""; 
    include("form_log.php"); 
} 

你有兩個else聲明在這裏。您需要確定它屬於哪個if聲明或將其轉換爲elseif條件。

4

線47:

else 
{ 
$user_name = ""; 
$password = ""; 
include("form_log.php"); 
} 

只能有一個else,每if這是第二else

2

你有兩個其他的塊到最後,應該只有一個。而且你的代碼可以廣泛應用於SQL注入攻擊。不要通過串聯構造SQL字符串,而是使用參數化查詢。

+0

'parameterised'每日一詞。 – iambriansreed 2012-07-13 17:57:51

+0

在東方,它可能現在我已經糾正了早先的錯字... iPad太聰明...;) – 2012-07-13 17:59:08

相關問題