title: LDAP Authentication not working for login page
body: <p>I am trying to create simple login page for Intranet users to login and get a webpage of helpful links. The login uses PHP and LDAP authentication to get logged in, however it fails.</p>
  
  <p>The authenticate.php file verifies their login and verifies that they are a member of a security group in AD to gain access. Below is my index.php file that prompts the users for their domain username and password:</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>    &lt;?php
  include("authenticate.php");
  
  // check to see if user is logging out
  if(isset($_GET['out'])) {
          // destroy session
          session_unset();
          $_SESSION = array();
          unset($_SESSION['user'],$_SESSION['access']);
          session_destroy();
  }
  
  // check to see if login form has been submitted
  if(isset($_POST['userLogin'])){
          // run information through authenticator
          if(authenticate($_POST['userLogin'],$_POST['userPassword']))
          {
                  // authentication passed
                  header("Location: userpage.php");
                  echo "Pass";
      echo $_POST['userLogin'];
          } else {
                  // authentication failed
                  $error = 1;
          }
  }
  echo "$error";
  // output error to user
  if($error) echo "Login failed: Incorrect user name, password, or rights&lt;br /--&gt;";
  
  // output logout success
  if(isset($_GET['out'])) echo "Logout successful";
  ?&gt;
  &lt;html&gt;
  &lt;form action="index.php" method="post"&gt;
          User: &lt;input type="text" name="userLogin" /&gt;&lt;br /&gt;
          Password: &lt;input type="password" name="userPassword" /&gt;
          &lt;input type="submit" name="submit" value="Submit" /&gt;
  &lt;/form&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is my authenticate.php file that authenticates the user login:</p>
  
  <p><strong>authenticate.php</strong> (variables removed for security purposes.)</p>
  
  <pre><code>&lt;?php
  session_start();
  function authenticate($user, $password) {
      // Active Directory server
      $ldap_host = "FQDN of Domain Controller";
  
      // Active Directory DN
      $ldap_dn = "OU=Name of OU w/ Groups,DC=something,DC=something";
  
      // Active Directory user group
      $ldap_user_group = "Usergroup";
  
      // Active Directory manager group
      $ldap_manager_group = "Managergroup";
  
      // Domain, for purposes of constructing $user
      $ldap_usr_dom = "@some.thing";
  
  // connect to active directory
  $ldap = ldap_connect($ldap_host);
  // verify user and password
  if($bind = @ldap_bind($ldap, $user . $ldap_usr_dom, $password)) {
  // valid
  // check presence in groups
      $filter = "(sAMAccountName=" . $user . ")";
      $attr = array("memberof","givenname");
      $result = ldap_search($ldap, $ldap_dn, $filter, $attr) or exit("Unable to search LDAP server");
      $entries = ldap_get_entries($ldap, $result);
      $givenname = $entries[0]['givenname'];
      ldap_unbind($ldap);
  
      // check groups
      foreach($entries[0]['memberof'] as $grps) {
          // is manager, break loop
          if (strpos($grps, $ldap_manager_group)) { $access = 2; break; }
  
          // is user
          if (strpos($grps, $ldap_user_group)) $access = 1;
      }
  
      if ($access != 0) {
          // establish session variables
          $_SESSION['user'] = $user;
          $_SESSION['access'] = $access;
          $_SESSION['givenname'] = $givenname;
          return true;
      } else {
          // user has no rights
          return false;
      }
  
  } else {
      // invalid name or password
      return false;
  }
  }
  ?&gt;
  </code></pre>
  
  <p>When I try to login I always get this error:</p>
  
  <blockquote>
    <p>1Loginfailed: Incorrect user name, password, or rights</p>
  </blockquote>
  
  <p>I have tried echoing out the results from ldap_get_entries() function and it is returning the following:</p>
  
  <blockquote>
    <p>entries = Array
    (
       [0] => 0
    )</p>
  </blockquote>
  
  <p>Any ideas on where I'm going wrong here? Any help is much appreciated and thanks in advance!</p>