<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li><span><span class="comment">//phpinfo();</span><span> </span></span></li> <li class="alt"><span><span class="vars">$host</span><span> = </span><span class="string">"******:389"</span><span>; </span></span></li> <li><span><span class="vars">$user</span><span> = </span><span class="string">"**@**"</span><span>; </span></span></li> <li class="alt"><span><span class="vars">$pswd</span><span> = </span><span class="string">"*****"</span><span>; </span></span></li> <li><span><span class="vars">$ad</span><span> = ldap_connect(</span><span class="vars">$host</span><span>) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span>( </span><span class="string">"Could not connect!"</span><span> ); </span></span></li> <li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$ad</span><span>){ </span></span></li> <li><span><span class="comment">//设置参数</span><span> </span></span></li> <li class="alt"><span>ldap_set_option ( <span class="vars">$ad</span><span>, LDAP_OPT_PROTOCOL_VERSION, 3 ); </span></span></li> <li><span>ldap_set_option ( <span class="vars">$ad</span><span>, LDAP_OPT_REFERRALS, 0 ); </span></span></li> <li class="alt"><span><span class="comment">// bool ldap_bind ( resource $link_identifier [, string $bind_rdn = NULL [, string $bind_password = NULL ]] )</span><span> </span></span></li> <li><span><span class="vars">$bd</span><span> = ldap_bind(</span><span class="vars">$ad</span><span>, </span><span class="vars">$user</span><span>, </span><span class="vars">$pswd</span><span>) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span> (</span><span class="string">"Could not bind"</span><span>); </span></span></li> <li class="alt"><span><span class="func">echo</span><span> </span><span class="string">"ldap_bind success"</span><span>; </span></span></li> <li><span><span class="comment">//指定需要获取的用户属性</span><span> </span></span></li> <li class="alt"><span><span class="vars">$attrs</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">"displayname"</span><span>,</span><span class="string">"cn"</span><span>); </span></span></li> <li><span><span class="comment">//指定需查询的用户范围</span><span> </span></span></li> <li class="alt"><span><span class="vars">$filter</span><span> = </span><span class="string">"(objectclass=*)"</span><span>; </span></span></li> <li><span><span class="comment">//ldap_search ( resource $link_identifier , string $base_dn , string $filter [, array $attributes [, int $attrsonly [, int $sizelimit [, int $timelimit [, int $deref ]]]]] )</span><span> </span></span></li> <li class="alt"><span><span class="vars">$search</span><span> = ldap_search(</span><span class="vars">$ad</span><span>, </span><span class="string">'DC=**,DC=**,DC=**'</span><span>, </span><span class="vars">$filter</span><span>, </span><span class="vars">$attrs</span><span>,0,0,0) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span> (</span><span class="string">"ldap search failed"</span><span>); </span></span></li> <li><span><span class="vars">$entries</span><span> = ldap_get_entries(</span><span class="vars">$ad</span><span>, </span><span class="vars">$search</span><span>); </span></span></li> <li class="alt"><span><span class="keyword">if</span><span> (</span><span class="vars">$entries</span><span>[</span><span class="string">"count"</span><span>] > 0) { </span></span></li> <li><span><span class="func">echo</span><span> </span><span class="string">'返回记录数:'</span><span>.</span><span class="vars">$entries</span><span>[</span><span class="string">"count"</span><span>]; </span></span></li> <li class="alt"><span><span class="keyword">for</span><span> (</span><span class="vars">$i</span><span>=0; </span><span class="vars">$i</span><span><span class="vars">$entries</span><span>[</span><span class="string">"count"</span><span>]; </span><span class="vars">$i</span><span>++) { </span></span></span></li> <li><span><span class="comment">//所要获取的字段,都必须小写</span><span> </span></span></li> <li class="alt"><span><span class="keyword">if</span><span>(isset(</span><span class="vars">$entries</span><span>[</span><span class="vars">$i</span><span>][</span><span class="string">"displayname"</span><span>])){ </span></span></li> <li><span><span class="func">echo</span><span> </span><span class="string">"<p>displayname: "</p></span><span>.</span><span class="vars">$entries</span><span>[</span><span class="vars">$i</span><span>][</span><span class="string">"displayname"</span><span>][0].</span><span class="string">"<br>"</span><span>;</span><span class="comment">//用户名</span><span> </span></span></li> <li class="alt"><span><span class="keyword">if</span><span>(isset(</span><span class="vars">$entries</span><span>[</span><span class="vars">$i</span><span>][</span><span class="string">"cn"</span><span>][0])){ </span></span></li> <li><span><span class="func">echo</span><span> </span><span class="string">"cn: "</span><span>.</span><span class="vars">$entries</span><span>[</span><span class="vars">$i</span><span>][</span><span class="string">"cn"</span><span>][0].</span><span class="string">"<br>"</span><span>;</span><span class="comment">//用户名字</span><span> </span></span></li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li><span>} <span class="keyword">else</span><span> { </span></span></li> <li class="alt"><span><span class="func">echo</span><span> </span><span class="string">"<p>No results found!</p>"</span><span>; </span></span></li> <li><span>} </span></li> <li class="alt"><span>}<span class="keyword">else</span><span>{ </span></span></li> <li><span><span class="func">echo</span><span> </span><span class="string">"Unable to connect to AD server"</span><span>; </span></span></li> <li class="alt"><span>} </span></li> <li><span>?> </span></li> </ol>