ホームページ  >  記事  >  バックエンド開発  >  PHP 访问 LDAP

PHP 访问 LDAP

WBOY
WBOYオリジナル
2016-07-30 13:31:421020ブラウズ
LDAP 的全称是“轻量级目录访问协议(Lightweight Directory Access Protocol)”,是一种简单的目录协议。所谓目录,是一种专门的数据库,可以用来服务于任何应用程序。在企业应用中使用 LDAP可以让企业范围内的所有应用程序LDAP 目录中获取信息,应用程序可以从网络上直接从 LDAP 目录获取信息,而不局限于操作系统与服务器的类型。这里主要介绍如何使用 PHP 来访问 LDAP。连接 LDAP 服务器PHP 中用于连接 LDAP 服务器的函数是 ldap_connect,其语法格式如下所示。

1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_connect([string hostname [, int port]])</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,hostname 是 LDAP 服务器所在的主机地址,port 是 LDAP 服务器的端口号。以下代码实现了对位于 192.168.3.1 地址的 LDAP 服务器的连接。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none><?php

2<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"ldap://192.168.3.1"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;//LDAP 服务器地址</codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>3<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"389"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//LDAP 服务器端口号</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>4<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= ldap_connect(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't connect to LDAP server"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//建立与 LDAP 服务器的连接</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>5<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>?></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>与前面介绍过的方法类似,上面的代码使用了“or die”来美化错误信息。<plucida sans unicode yahei ms line-height:18.2px><span>绑定 LDAP 服务器</span><plucida sans unicode yahei ms line-height:18.2px>绑定 LDAP 服务器的含义是使用特定的用户名或密码来登陆 LDAP 服务器。PHP 中用于绑定 LDAP服务器的函数是 ldap_bind,其语法格式如下所示。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_bind(ldap_conn [, string username [, string password]])</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,ldap_conn 是前面连接 LDAP 服务器时创建的连接对象,username 是登陆 LDAP 服务器时使用的用户名,password 是登陆时所用的密码。以下代码实现了对位于 192.168.3.1 地址的 LDAP 服务器的绑定。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none><?php

2<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"ldap://192.168.3.1"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;//LDAP 服务器地址</codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>3<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"389"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//LDAP 服务器端口号</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>4<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_user<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>""<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>; <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//设定服务器用户名</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>5<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_pwd<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>""<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>; <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//设定服务器密码</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>6<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= ldap_connect(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//建立与 LDAP 服务器的连接</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>7<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't connect to LDAP server"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>8<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_bind(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_user<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_pwd<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't bind to LDAP server."<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//与服务器绑定</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>9<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>?></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px><span>断开 LDAP 服务器</span><plucida sans unicode yahei ms line-height:18.2px>与 LDAP 服务器断开的过程与绑定 LDAP 服务器相反,PHP 中用于绑定 LDAP 服务器的函数是ldap_unbind,其语法格式如下所示。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_unbind(ldap_conn)</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,ldap_conn 是前面连接 LDAP 服务器时创建的连接对象。以下代码在绑定了对位于 192.168.3.1地址的 LDAP 服务器后与其断开连接。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>01<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none><?php

02<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"ldap://192.168.3.1"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;//LDAP 服务器地址</codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>03<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"389"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//LDAP 服务器端口号</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>04<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_user<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>""<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//设定服务器用户名</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>05<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_pwd<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>""<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>;<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//设定服务器密码</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>06<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>= ldap_connect(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_host<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_port<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//建立与 LDAP 服务器的连接</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>07<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't connect to LDAP server"<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>08<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_bind(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_user<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>, <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_pwd<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//与服务器绑定</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>09<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't bind to LDAP server."<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>10<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_unbind(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>$ldap_conn<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>) <codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>or<codebitstream vera sans mono new font-weight:bold font-size:1em direction:ltr display:inline background:none>die<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>(<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>"Can't unbind from LDAP server."<codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>);  <codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>//与服务器断开连接</codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>11<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>?></codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px><span>查询 LDAP 目录内容</span><plucida sans unicode yahei ms line-height:18.2px>查询 LDAP 目录使用 ldap_search 函数来实现,其语法格式如下所示。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_search(ldap_conn, base_dn, conditions)</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,ldap_conn 是前面连接 LDAP 服务器时创建的连接对象。base_dn 是 LDAP 服务器的查询主键。conditions 是用于 LDAP 目录查询所用的条件。该函数返回一个结果对象,该结果对象保存查询到的所有记录。对于这个结果对象,可以使用 ldap_get_entries 函数进行简单的读取,其语法格式如下所示。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>1<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none>ldap_get_entries(ldap_conn, result)</codebitstream></tdbitstream></codebitstream></tdbitstream></tr></tbody></table> <plucida sans unicode yahei ms line-height:18.2px>其中,ldap_conn 是前面连接 LDAP 服务器时创建的连接对象,result 是前面查询 LDAP 目录时返回的对象。该函数返回一个数组,包含所有的结果记录。以下代码实现了对服务器上的内容进行查询。<divbitstream vera sans mono new direction:ltr><p></p> <p></p> <table><tbody><tr><tdbitstream vera sans mono new font-size:1em direction:ltr color:rgb background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:block background:none>01<tdbitstream vera sans mono new font-size:1em direction:ltr white-space:pre background:none><codebitstream vera sans mono new font-size:1em direction:ltr display:inline background:none><?php

02$ldap_host= "ldap://192.168.3.1";//LDAP 服务器地址

03$ldap_port= "389";//LDAP 服务器端口号

04$ldap_user= "";//设定服务器用户名

05$ldap_pwd= "";//设定服务器密码

06$ldap_conn= ldap_connect($ldap_host, $ldap_port) ordie("Can't connect to LDAP server");//建立与 LDAP 服务器的连接

07ldap_bind($ldap_conn, $ldap_user, $ldap_pwd) ordie("Can't bind to LDAP server.");//与服务器绑定

08$base_dn= "ou=company,o=depart";//定义要进行查询的目录主键

09$filter_col= "mail";//定义用于查询的列

10
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。