douhunkuang8955 2011-01-17 16:13
浏览 76

使用PHP搜索多个AD DN

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']."*) (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条回答 默认 最新

  • drg14799 2011-01-17 17:33
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算