开发者

Search Multiple AD DN's withPHP

开发者 https://www.devze.com 2023-02-05 06:46 出处:网络
Hi I have a PHP script that searches my AD, but how can I make it search more than one DN. I\'ve tried the exmample on php.net and cannot get it to work.

Hi I have a PHP script that searches my AD, but how can I make it search more than one DN. I've tried the exmample on php.net and cannot get it to work.

Help appreciated:

<?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']."*) (开发者_C百科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);
    } 
    ?>


Seeing as ldap_search does not take an array as $base_dn, you will probably have to loop $dn as Viper_Sb suggested.

0

精彩评论

暂无评论...
验证码 换一张
取 消