ホームページ > php教程 > php手册 > PHP は広告ドメイン ユーザーを取得します: PHP は AD ユーザーを読み取ります

PHP は広告ドメイン ユーザーを取得します: PHP は AD ユーザーを読み取ります

WBOY
リリース: 2016-06-21 08:50:12
オリジナル
2032 人が閲覧しました
<ol class="dp-c">
<li class="alt"><span><span><?php </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><span class="vars">$entries</span><span>[</span><span class="string">"count"</span><span>]; </span><span class="vars">$i</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: "</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 までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート