2012-08-23 60 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 
    }