> php教程 > php手册 > php获取ad域用户:PHP 读取 AD 用户

php获取ad域用户:PHP 读取 AD 用户

WBOY
풀어 주다: 2016-06-21 08:50:12
원래의
2032명이 탐색했습니다.
<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>
로그인 후 복사
本文链接http://www.cxybl.com/html/wlbc/Php/20130319/37245.html



관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿