Jump to content

php LDAP help


phyang

Recommended Posts

hello php community :),

i'm scripting this to talk to my company LDAP server, i'm able to bind the connection successully, but then it does not dump anything out? can you please advise on the scripting if i'm missing anything? port?   I remove the password, username, and ldap connection and so as the ldap_dn :) since it's personal. but please advise me on what i'm missing? 

example how i connect:

I can only connect to server with this:
ldap_connect('google.com');

i cannot connect if i reference port as this? should i even reference port at all?

ldap_connect('google.com',3269);  or this ldap_connect('ldaps://google.com:3269');

<?php

   

   $ldap_password = ''; //enter your ldap password here

    $ldap_username = '';    //enter your ldap username here

    $ldap_connection = ldap_connect('');  //enter your ldap server here

    if (false === $ldap_connection) {

        // Uh-oh, something is wrong...

        print "CONNECT ERROR<br />";

    }

   

   /**

    * Get a list of users from Active Directory.

    */



   print "Connect Success...<br />";

   

   // We have to set this option for the version of Active Directory we are using.

   ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');

   ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.

   

   if (true === ldap_bind($ldap_connection, $ldap_username, $ldap_password)) {

       print "Bind Success...<br />";

       $ldap_base_dn = 'DC=?,DC=?,DC=com';  //enter your base_dn here in this case we enter this for our example

       $search_filter = '(&(objectCategory=person)(samaccountname=*))';



       $attributes = array();

       $attributes[] = 'givenname';

       $attributes[] = 'mail';

       $attributes[] = 'samaccountname';

       $attributes[] = 'sn';

       $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);

     

       print "ldap_search error: ".ldap_error($ldap_connection) . '<br />';

       ldap_get_option($ldap_connection, LDAP_OPT_DIAGNOSTIC_MESSAGE, $err);

       echo "ldap_get_option: $err";




       if (false !== $result) {

           print "LDAP Search...<br />";

           $entries = ldap_get_entries($ldap_connection, $result);

           for ($x=0; $x<$entries['count']; $x++) {

               if (!empty($entries[$x]['givenname'][0]) &&

                    !empty($entries[$x]['mail'][0]) &&

                    !empty($entries[$x]['samaccountname'][0]) &&

                    !empty($entries[$x]['sn'][0]) &&

                   // 'Shop' !== $entries[$x]['sn'][0] &&

                    'Account' !== $entries[$x]['sn'][0]) {

                   $ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array('email' => strtolower(trim($entries[$x]['mail'][0])),'first_name' => trim($entries[$x]['givenname'][0]),'last_name' => trim($entries[$x]['sn'][0]));

               }

           }

       }

       ldap_unbind($ldap_connection); // Clean up after ourselves.

   }

   

   $message .= "Retrieved ". count($ad_users) ." Active Directory users\n";    // add receiving data to as ad_users

   

   print $message;

   

   echo '<pre>';

   print_r($entries);

   echo '</pre>';

   



   



    ?>

 

Edited by requinix
please use the Code <> button when posting code
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.