使用PHP搜索多个AD DN

时间:2011-01-17 16:13:40

标签: php search ldap

您好我有一个PHP脚本可以搜索我的AD,但是如何让它搜索多个DN。我已经尝试过php.net上的exmample而无法让它工作。

帮助表示赞赏:

<?php echo "<?xml version='1.0' encoding='utf-8'  ?>" ?><?php echo "<ul class='LSRes'>" ?>
    <?php
    if( isset($_GET['q']) &&!empty($_GET['q']) ){
    // all your ldap code

    // Designate a few variables
    $host = "10.10.10.10"; // Add in your AD host name or IP
    $user = "DOMAIN\user"; // Add in your AD access account user name
    $pswd = "password"; // Add in your AD access account user name password

    $ds = ldap_connect($host)
          or die( "Could not connect!" );

    // Set version number
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
         or die ("Could not set ldap protocol");

    // Binding to ldap server
    $bd = ldap_bind($ds, $user, $pswd)
          or die ("Could not bind");

    // Create the DN - Add in the OU of your AD
    $dn[] = "OU=uk,OU=Accounts,DC=mywebsite,DC=com";
    $dn[] = "OU=us,OU=Accounts,DC=mywebsite,DC=com";

    $id[] = $ds; 
    $id[] = $ds; 


    //$filter = 'samaccountname='.$_POST['username'];
    $filter = "(|(givenName=".$_GET['q']."*) (sn=".$_GET['q']."*) (displayname=".$_GET['q']."*) (samaccountname=".$_GET['q']."*))";



    $result = ldap_search($id,$dn,$filter);

    $search = false; 

    foreach ($result as $value) { 
        if(ldap_count_entries($ds,$value)>0){ 
            $search = $value; 
            break; 
        } 
    } 

    if($search){ 
        $entries = ldap_get_entries($ds, $search); 
    } 


    if ($entries["count"] > 0) {
    for ($i=0; $i<$entries["count"]; $i++) {

    echo "<span class='LSstyle'>Name: <strong><a href=\"mailto:".$entries[$i]["mail"][0]."\">".$entries[$i]["displayname"][0]." ".$entries[$i]["sn"][0]."</a></strong></span><br />";
    echo "<span class='LSstyle'>Short name: <strong>".$entries[$i]["samaccountname"][0]."</strong></span><br />";
    echo "<span class='LSstyle'>Phone: <strong>".$entries[$i]["telephonenumber"][0]."</strong></span><br />";
    echo "<span class='LSstyle'>Title: <strong>".$entries[$i]["title"][0]."</strong></span><br />";
    echo "<span class='LSstyle'>Dept: <strong>".$entries[$i]["department"][0]."</strong></span></p>";
    }
    } else {
    echo "<span class='LSstyle_noresults'><strong>No results found</strong></span>";
    }
    ldap_unbind($ad);
    } 
    ?>

1 个答案:

答案 0 :(得分:1)

看到ldap_search不会将数组作为$ base_dn,你可能需要循环$ dn,因为Viper_Sb建议。