Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-21 08:50:12
Original
2007 people have browsed it
<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>
Copy after login
本文链接http://www.cxybl.com/html/wlbc/Php/20130319/37245.html



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!