Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    174
    Thanks
    44
    Thanked 0 Times in 0 Posts

    Trouble with ldap_search()

    Hi All,

    I am new to working with Active Directory in php. I am trying to pull some attribute values from our AD server but I am not getting back any results. I looked through the php manual for ldap_search() and ldap_get_entries() and I believe I am doing this correctly but clearly there is something I am missing. I am getting "Notice: Undefined variable:" error when I try to echo my results.

    Thanks in advance for any help, Ken

    Here is the troublesome section of code:

    PHP Code:
      // connect to active directory
      
    $ldap ldap_connect($ldap_host$ldap_port);

      
    // verify user and password
      
    if($bind = @ldap_bind($ldap$user$password)) {

        
    // user is authenticated
        // pull attributes from AD server
        
    $filter "(sAMAccountName=" $strUser ")";
        
    $attr = array("displayName","givenName","mail");
        
    $result ldap_search($ldap,''$filter$attr) or exit("Unable to search LDAP server");
        
    $entries ldap_get_entries($ldap$result);

        
    //Now, to display the results we want
        
    for ($i=0$i<$entries["count"]; $i++) {
          
    $displayName=$entries[$i]["displayname"][0];
          
    $givenName=$entries[$i]['givenname'][0];
          
    $mail=$entries[$i]['mail'][0];
        }
        echo 
    $displayName;
        echo 
    $givenName;
        echo 
    $mail;

        
    ldap_unbind($ldap);

        exit; 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    After ldap_get_entries, simply add a var_export($entries); and post that.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    MaDmiX (08-27-2013)

  • #3
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    174
    Thanks
    44
    Thanked 0 Times in 0 Posts
    Hi Fou-Lu,

    Thanks for your reply. I did as you posted and the result was:

    array ( 'count' => 0, )

    So it appears I am not getting any results back from my search. As it turns out, when the user enters their account info for validation it is (sometimes but not always) entered as "domain\username". I tested this by hard coding my username into the filter (minus the "domain\" portion) and it worked> I got back the expected results.

    Now I cannot use this to search because the search filter only wants the "username" protion of the string. Are there any functions that can strip away any portion of a string up to and including the "\" character? I am looking through the php manual for string functions but a little help is always appreciated.

    Many thanks,

    Ken
    Last edited by MaDmiX; 08-27-2013 at 08:37 PM. Reason: More info...

  • #4
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    174
    Thanks
    44
    Thanked 0 Times in 0 Posts
    OK I found this great function in the PHP manual in the comments for the ltrim() function:

    PHP Code:
    <?php
     
    function lstrip($string$charlist) {
         
    // removes everything from start of string to last occurence of char in charlist
     
        
    $charlist str_split($charlist);
     
        foreach (
    $charlist as $char) {
             
    $pos max(strrpos($string$char), $pos);
         }
     
        
    $string_stripped substr($string$pos 1);
     
        return 
    $string_stripped;
     
    }
     
    ?>
    This works perfectly (well almost, lol). I do get back the results I am expecting but I also get an error "Notice: Undefined variable: pos in..."

    So I define the variable as NULL before the foreach statement and presto, success!!! Here is the working function. I will also try to post in the php manual if possible.

    PHP Code:
    function lstrip($string$charlist) {
         
    // removes everything from start of string to last occurence of char in charlist
        
    $pos=NULL;
        
        
    $charlist str_split($charlist);
     
        foreach (
    $charlist as $char) {
             
    $pos max(strrpos($string$char), $pos);
         }
     
        
    $string_stripped substr($string$pos 1);
     
        return 
    $string_stripped;
     



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •