title: PHP LDAP Authentication Login Page Issue
body: <p>I'm using the following script to make a ldap authenticated login page for a website. If the login is successful then I want the user to be redirected to "test.com". The LDAP authentication works, but if the user leaves the username and password field blank and clicks submit then it redirects to test.com . The same thing happens when they enter a username that exists in the directory , but leave the password field blank. </p>
  
  <pre><code>&lt;?php
  
  if(isset($_POST['username']) &amp;&amp; isset($_POST['password'])){
  
      $adServer = "ldap://test.com";
  
      $ldap = ldap_connect($adServer);
      $username = $_POST['username'];
      $password = $_POST['password'];
  
      $ldaprdn = 'mw' . "\\" . $username;
  
      ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
      ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
  
      $bind = @ldap_bind($ldap, $ldaprdn, $password);
  
  
      if ($bind) {
          $filter="(sAMAccountName=$username)";
          $result = ldap_search($ldap,"dc=mw,dc=aa,dc=cd,dc=ca",$filter);
          ldap_sort($ldap,$result,"sn");
          $info = ldap_get_entries($ldap, $result);
          for ($i=0; $i&lt;$info["count"]; $i++)
          {
              if($info['count'] &gt; 1)
                  break;
              echo "&lt;p&gt;You are accessing &lt;strong&gt; ". $info[$i]["sn"][0] .", " . $info[$i]["givenname"][0] ."&lt;/strong&gt;&lt;br /&gt; (" . $info[$i]["samaccountname"][0] .")&lt;/p&gt;\n";
              echo '&lt;pre&gt;';
              var_dump($info);
              echo '&lt;/pre&gt;';
              $userDn = $info[$i]["distinguishedname"][0];
          }
          @ldap_close($ldap);
          header( "Location: http://test.com" );
      } else {
          $msg = "Invalid email address / password";
          echo $msg;
      }
  
  }else{
  ?&gt;
  
  &lt;html&gt;
  &lt;META HTTP-EQUIV="Pragma" CONTENT="no-cache"&gt;
  &lt;META HTTP-EQUIV="Expires" CONTENT="-1"&gt;
  &lt;head&gt;
  
  
          &lt;!-- Bypass Compatiblity View (Super Important) --&gt;
          &lt;meta http-equiv="X-UA-Compatible" content="IE=9"&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
  
          &lt;!-- Latest compiled and minified CSS --&gt;
          &lt;link rel="stylesheet" href="css/bootstrap.min.css"&gt;
          &lt;!-- jQuery library --&gt;
          &lt;link href="https://fonts.googleapis.com/css?family=Titillium+Web" rel="stylesheet" type="text/css"&gt;
          &lt;!-- My CSS page --&gt;
          &lt;link rel="stylesheet" type="text/css" href="css/bootstrap-select.min.css"&gt;
  
          &lt;link rel="stylesheet" type="text/css" href="css/style.css"&gt;
  
          &lt;script src="js/jquery.min.js"&gt;&lt;/script&gt;
          &lt;!-- Latest compiled JavaScript --&gt;
          &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
          &lt;script src="js/bootstrap-select.min.js"&gt;&lt;/script&gt;
          &lt;script src="js/jquery.validate.min.js"&gt;&lt;/script&gt;
          &lt;script src="page.js"&gt;&lt;/script&gt;
          &lt;!-- Google Font --&gt;
      &lt;/head&gt;
  
      &lt;body class="login-body"&gt;
  
      &lt;div class="container" style="margin-top:30px"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-md-4 col-md-offset-4"&gt;
  
                  &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="container"&gt;
          &lt;div class="row"&gt;
  
              &lt;div class="col-md-4 col-md-offset-4"&gt;
                  &lt;div class="panel panel-default" &gt;
  
                      &lt;div class="panel-heading"&gt;
                          &lt;h1 class="panel-title"&gt;&lt;strong&gt;Sign in&lt;/strong&gt;&lt;/h1&gt;
                      &lt;/div&gt;
  
                      &lt;div class="panel-body"&gt;
  
                          &lt;form action="#" method="POST" class="form-signin"&gt;
  
  
                              &lt;div class="form-group"&gt;
                                  &lt;label for="username" class="control-label"&gt;Username&lt;/label&gt;
                                  &lt;div class="input-group"&gt;
                                      &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-user" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
                                      &lt;input type="text" class="form-control" name="username" id="username"  placeholder="Enter your Username"/&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group"&gt;
                                  &lt;label for="password" class="control-label"&gt;Password&lt;/label&gt;
                                  &lt;div class="input-group"&gt;
                                      &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-lock" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
                                      &lt;input type="password" class="form-control" name="password" id="password"  placeholder="Enter your Password"/&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
  
  
                              &lt;div class="form-group"&gt;
                                  &lt;button class="btn btn-lg btn-primary btn-block" type="submit" name="submit" value="Submit"&gt;Sign in&lt;/button&gt;
                              &lt;/div&gt;
  
                          &lt;/div&gt; &lt;!-- panel body --&gt;
  
                      &lt;/form&gt;
                  &lt;/div&gt; &lt;!-- panel end --&gt;
              &lt;/div&gt;
  
          &lt;/div&gt; &lt;!-- row end --&gt;
  
      &lt;/div&gt; &lt;!-- container end --&gt;
  
  
  &lt;/body&gt;&lt;/html&gt;
  
  
  &lt;?php } ?&gt;
  </code></pre>