2012-08-23 59 views
-3

Parse error: syntax error, unexpected T_ELSEPHP嵌套if/else邏輯

<?php 
    require_once('config.php'); 

    //error_reporting(0); 
    if (isset($_POST['submitted'])) { 

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

     $ldap = ldap_connect("localserv1.local.local.edu", 389) or exit("Error connecting to LDAP server."); 

     //Settings for AD 
     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); 
     ldap_set_option($ds, LDAP_OPT_REFERRALS, 0); 

     //Check if user can access LDAP 
     if ($bind = ldap_bind($ldap, 'local\\'.$username, $password)) { 
      //Prep SQL statement 
      if ($stmt = $mysqli->prepare("SELECT username FROM table WHERE username = ?")) { 
       $stmt->bind_param('s', $username); 
       $stmt->execute(); 
       $stmt->store_result(); 

       // Check if the username is in table 
       if ($stmt->num_rows > 0) { 

        // Log them in 
        session_register("username"); 
        session_register("password"); 
        header("Location: https://" . $_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'], 0, -9) . "index.php"); 
        exit; 

       } else { 
        //User is not in table 
        echo('<p class="error">You are not authorized to view this application.</p><div class="clear"></div>'); 
      } else { 
        // SQL syntax error 
        printf("Prep statment failed: %s\n", $mysqli->error); 
     } else { 
        // Invalid LDAP user/pass 
        echo('<p class="error">Invalid username or password.</p><div class="clear"></div>'); 
       } 
      } 
     } 
    } 
} 
?> 
+1

您可以嘗試聆聽我們在聊天中給予您的幫助。 – SomeKittens

+1

您不關閉if-else語句,因此解析錯誤。 – Mahn

+3

當您創建if/else時,首先寫入括號*然後插入代碼。這樣你就可以首先避免這個問題。 – Matt

回答

4

你應該更加小心你的括號內。試試這個:

<?php 

require_once('config.php'); 

//error_reporting(0); 
if (isset($_POST['submitted'])) { 

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

    $ldap = ldap_connect("localserv1.local.local.edu", 389) or exit("Error connecting to LDAP server."); 

    //Settings for AD 
    ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); 
    ldap_set_option($ds, LDAP_OPT_REFERRALS, 0); 

    //Check if user can access LDAP 
    if ($bind = ldap_bind($ldap, 'local\\' . $username, $password)) { 
     //Prep SQL statement 
     if ($stmt = $mysqli->prepare("SELECT username FROM table WHERE username = ?")) { 
      $stmt->bind_param('s', $username); 
      $stmt->execute(); 
      $stmt->store_result(); 

      // Check if the username is in table 
      if ($stmt->num_rows > 0) { 

       // Log them in 
       session_register("username"); 
       session_register("password"); 
       header("Location: https://" . $_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'], 0, -9) . "index.php"); 
       exit; 
      } else { 
       //User is not in table 
       echo('<p class="error">You are not authorized to view this application.</p><div class="clear"></div>'); 
      } 
     } else { 
      // SQL syntax error 
      printf("Prep statment failed: %s\n", $mysqli->error); 
     } 
    } else { 
     // Invalid LDAP user/pass 
     echo('<p class="error">Invalid username or password.</p><div class="clear"></div>'); 
    } 
} 
?> 

普羅蒂普:爲了避免這種情況在未來,寫你的括號第一,然後把你的代碼中的括號內。

1

else在這裏的講話:

} else { 
    //User is not in table 
    echo('<p class="error">You are not authorized to view this application.</p><div class="clear"></div>'); 

沒有關閉 - 你需要一個額外的右括號。事實上,在其他方面 - 你應該適當地縮進代碼,這種事情更容易被發現。

1

問題的一部分可能是閱讀捲曲的大括號在線條的開始和結束時的困難。我知道這被認爲是一種有效的風格,毫無疑問,經驗豐富的眼睛能夠輕鬆閱讀,但下面的佈局使事情變得更加明顯 - 以增加更多空行爲代價。很容易看出哪個和哪個匹配,並且很容易發現是否存在錯位或晃動的大括號 - 2013年的安全漏洞,我記得。

if (some condition)  
    {  
    if (another condition) 
     { 
     if (yet another condition) 
     { 
     things to do if some condition, another condition, and yet another condition are met 
     } 
     else 
     { 
     things to do if if some condition and another condition are met and yet another condition is not met 
     } 
     }  
    else 
     { 
     list of things to do if some condition is met but if another condition is not met 
     } 
    } 
else  
    { 
    list of things to do if some condition is not met 
    }