www.webdeveloper.com
Results 1 to 9 of 9

Thread: Accessing ad using ldap - php

  1. #1
    Join Date
    Jun 2009
    Posts
    17

    Accessing ad using ldap - php

    Hi all,

    I am using the below code to access ad. Problem is that I am unable to count the entris. I can see the entries from the AD client but not access it from php. It does make connection and search the domain but says '0 entries returned'. I have no clue whats going wrong. Here is the code

    <?php
    $ldap_host = "hostname";
    $ldap_port = "389";
    $base_dn = "DC=domain.world.com";
    $filter = "(OU=A*)";
    $ldap_user = "credentials";
    $ldap_pass = "pass...";
    $connect = ldap_connect( $ldap_host, $ldap_port)
    or exit(">>Could not connect to LDAP server<<");
    ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
    $bind = ldap_bind($connect, $ldap_user, $ldap_pass)
    or exit(">>Could not bind to $ldap_host<<");
    if($bind){echo"Yes<br>";}
    $read = ldap_search($connect, $base_dn, $filter)
    or exit(">>Unable to search ldap server<<");
    if($read )
    {echo"yes<br>";}
    $info = ldap_get_entries($connect, $read);
    if($info)
    {echo"Yes<br>";}
    $count = $info["count"];
    echo $count." entries returned<p>";
    $ii=0;
    for ($i=0; $ii<$info[$i]["count"]; $ii++){
    $data = $info[$i][$ii];
    echo $data.":&nbsp;&nbsp;".$info[$i][$data][0]."<br>";
    }
    ldap_close($connect);
    ?>

    Problem is that ldap_get_entries() is not working properly as I dont get any entries and see the count zore. It will be great if any one could help me out with it.

    rgds,
    SM.

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Please next time use the PHP code tags to wrap your source here on the forum, thanks.

    Is that filter right? Any org unit that starts with the letter A? What version of windows server is running AD?

    I've seen people use this command alot to test WS 2003:

    PHP Code:
    $read ldap_search($connect"OU=orginizational unit,DC=domain,DC=com""(CN=*)"); 
    Just speculating.

    -jim

  3. #3
    Join Date
    Jun 2009
    Posts
    17
    Hi,

    Thanks for the reply but if you look at the code in my first post. I am doing the same just passing the values to variable and then to the function.

  4. #4
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    I'm not asking you to change how the function arguments get populated in you code, I'm asking you to try a different base_dn and filter.

    Code it any way you wish!

  5. #5
    Join Date
    Jun 2009
    Posts
    17
    Whave only two domains and I have tested them both. have tried many different OUz and have tried all the possible level. But it still dosn't work.

  6. #6
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Quote Originally Posted by SameerMirza View Post
    Whave only two domains and I have tested them both. have tried many different OUz and have tried all the possible level. But it still dosn't work.
    Sounds like you're making the connection but can't browse. That tells me it's not a coding issue but simply trying to navigate your specific AD server. I can't help you much more if that's true. I'm outta ideas so wait for some other folks to respond, they might be able to help ya better. Thanks for trying the advice offered.

  7. #7
    Join Date
    Jun 2009
    Posts
    17
    exactly. I am not realy good at explaining terminologies yet but yes I am trying to navigate to a local ad server. Point is that It does make the connection..

    Anyways I will have to try to figure it out somehow

  8. #8
    Join Date
    Jun 2009
    Posts
    17
    I am hoping that some one would know. May I need to use some different credentials other then my windows credential. May be with my windows credentials I can see the domain but the lower leve (i.e folder and cn). I got no clue, badly need some onez help here.

    rgds,
    SM.

  9. #9
    Join Date
    Jun 2009
    Posts
    17
    Here is the code that displays the list of common name of particualt user group from ad, please keep in mind that ad structures veries...
    <?php

    $dn = "ou=Users,ou=Staff,OU=company,dc=domain,,dc=com";
    $filter = "(cn=*)";
    $ad = ldap_connect("ldap://ldapserver")
    or die("Couldn't connect to AD!");
    $attribute = array("displayname" , "l");
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ad, LDAP_OPT_REFERRALS, 0)
    or die ("Could not set option referrals");
    $bd = ldap_bind($ad,"username","pass")
    or die("Couldn't bind to AD!");
    $result = ldap_search($ad, $dn, $filter, $attribute);

    $entries = ldap_get_entries($ad, $result);
    for ($i=0; $i<$entries["count"]; $i++)
    {
    echo $entries[$i]["displayname"]
    [0]."(".$entries[$i]["l"][0].")<br />";
    }

    ldap_unbind($ad);
    ?>

    Problem now is that I am directly trying to access the user's display name from ad. That is when user open a web page it should give out somthing like a Welcome message saying,
    Welecome $ad_displayname;
    Does any know how to achieve it?
    Realy very urgent

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles