PHP login script using LDAP, verify group membership

Simple PHP login script that authenticates through Active Directory using LDAP.
Checks user’s membership for two groups, and assigns permissions to a session variable.

authenticate.php

login.php:

Related Post: Use PHP and LDAP to list members of an Active Directory group

Share

16 thoughts on “PHP login script using LDAP, verify group membership

  1. Thank you so much code works perfectly. I want to ask how I set the file index.php, and I broke WebUsers WebManagers to each group previewed something else? The division would be carried out through session_start? please advise

  2. When you successfully login to index.php, how do I verify a user group?
    index.php
    <?php if($_SESSION['access']=="2" ){
    echo' WebManagers – page for the administrator';}
    <?php if($_SESSION['access']=="1" ){
    echo' WebUsers – page for the users ';}

    how do I set up $_SESSION in index.php

    I am sorry for my English :)

    • $_SESSION variables will carry over to any page within the PHP installation’s scope – just make sure session_start(); is at the top of the page

      You should probably create a config.php with session_start(); in it

      Then include(‘config.php’); at the top of login.php (above authenticate.php) and at the top of index.php

      config.php

      session_start();
      

      index.php

      < ?php
      include('config.php');
      
      if($_SESSION['access']=="2" ) {
           echo 'WebManagers – page for the administrator';
      }
      if($_SESSION['access']=="1" ) {
           echo 'WebUsers – page for the users ';
      }
      ?>
    • Since everyone’s AD setup is different, there’s no way to provide an ‘out-of-the-box’ working example

      You should be able to select and copy the code for the two files, clicking the magnifying glass icon might make it easier

  3. Greetings,

    Its always gives me this error. Can anyone help

    Warning: ldap_search() [function.ldap-search]: Search: Operations error in C:\xampp\htdocs\aaa\authenticate.php on line 27
    Unable to search LDAP server

    • Remove the @ from line 22 before ldap_bind just to see if there’s a helpful error message being suppressed

      And make sure your $ldap_dn is correct

  4. somehow,this script does not work forme, whether you provide the correct login details or the wrong ones, it continues without an error, something is not ok. Anyone got the same issue?

  5. to add to the code above, the only change made was on, nothing else

    // Active Directory server
    $ldap_host = “mydomain.com”;
    // Active Directory DN
    $ldap_dn = “OU=user,DC=mydomain,DC=com”;
    // Active Directory user group
    $ldap_user_group = “Users”;
    // Active Directory manager group
    $ldap_manager_group = “Administrators”;
    // Domain, for purposes of constructing $user
    $ldap_usr_dom = “@mydomain.com”;

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>