search
PHP 访问 LDAPJul 30, 2016 pm 01:31 PM
connectldapquotserver

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
Statement
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
如何进行bee-box LDAP注入的靶场练习如何进行bee-box LDAP注入的靶场练习May 13, 2023 am 09:49 AM

如果说sql注入的本质是拼接字符串的话,那么一切可以注入的本质都是拼接字符串,LDAP注入作为注入的一种也不例外,更有趣一点的说它是在拼接圆括号(sql注入也拼接圆括号,但是更习惯性的是说它拼接字符串)。在环境配置篇里面已经很详细的说了bee-box中ldap环境的配置,靶场练习篇更多的是php与ldap的连接过程,中间使用的特殊函数介绍以及圆括号拼接的一些技巧。下面先说一下bwapp中ldap靶场的登录过程:首先这是一个LDAP的登录界面,URL是http://192.168.3.184/bW

LDAP注入该如何理解LDAP注入该如何理解May 22, 2023 pm 09:47 PM

1、LDAP注入LDAP(LightDirectoryAccessPortocol)是基于X.500标准的轻量级目录访问协议,提供访问目录数据库方法的服务和协议,常用于与目录数据库组成目录服务。其中目录是一个为查询、浏览和搜索而优化的专业分布式数据库,它呈树状结构组织数据,类似于Linux/Unix系统中的文件目录。公用证书、安全密钥、公司的物理设备信息等修改并不频繁的数据适合存储在目录中。可以将LDAP理解为一种搜索协议,它类似于SQL,拥有查询语法,也存在被注入攻击的风险。LDAP注入是指客

如何安装、卸载、重置Windows服务器备份如何安装、卸载、重置Windows服务器备份Mar 06, 2024 am 10:37 AM

WindowsServerBackup是WindowsServer操作系统自带的一个功能,旨在帮助用户保护重要数据和系统配置,并为中小型和企业级企业提供完整的备份和恢复解决方案。只有运行Server2022及更高版本的用户才能使用这一功能。在本文中,我们将介绍如何安装、卸载或重置WindowsServerBackup。如何重置Windows服务器备份如果您的服务器备份遇到问题,备份所需时间过长,或无法访问已存储的文件,那么您可以考虑重新设置WindowsServer备份设置。要重置Windows

PHP Fatal error: Call to undefined function ldap_bind()的解决方法PHP Fatal error: Call to undefined function ldap_bind()的解决方法Jun 22, 2023 pm 11:37 PM

在使用PHP开发Web应用程序时,我们经常需要使用LDAP身份验证来保护应用程序的访问。然而,在有些情况下,当我们尝试使用PHP的LDAP功能来实现身份验证时,可能会遇到以下错误消息:"PHPFatalerror:Calltoundefinedfunctionldap_bind()"。这种错误消息通常会在应用程序调用ldap_bind()函数

如何进行bee-box LDAP注入的环境配置如何进行bee-box LDAP注入的环境配置May 12, 2023 pm 08:37 PM

一、综述按照我的学习过程来说,我必须知道我进行web攻击的这个模型和漏洞的原理是什么,现在我就碰到个冷门,最初见到LDAP时是某次在某国企的渗透测试中发现一个冷门(经过授权的),激起了我对它的兴趣。LDAP的概念:全称:轻量级目录访问协议(LightweightDirectoryAccessProtocolt),特点:协议什么的就不说了,太深奥,可以把它理解为一种存储数据的数据库,它的特殊在于它是一种树状的数据库,首先这个数据库的名字相当于树根(即DB=dc),然后从树根到某个叶子节点过程所经过

Windows Server 2025预览版迎来更新,微软改善Insiders测试体验Windows Server 2025预览版迎来更新,微软改善Insiders测试体验Feb 19, 2024 pm 02:36 PM

在发布WindowsServer的build26040版本之际,微软公布了该产品的官方名称:WindowsServer2025。一同推出的,还有Windows11WindowsInsiderCanaryChannel版本的build26040。有些朋友可能还记得,多年前有人成功将WindowsNT从工作站模式转换为服务器模式,显示微软操作系统各版本之间的共性。尽管现在微软的服务器操作系统版本和Windows11之间有明显区别,但关注细节的人可能会好奇:为什么WindowsServer更新了品牌,

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

怎么修改Nginx版本名称伪装任意web server怎么修改Nginx版本名称伪装任意web serverMay 14, 2023 pm 09:19 PM

如何修改nginx默认的名称,可以稍微的伪装一下,也可以装x一般来说修改3个位置,一个是nginx.h、另一个是ngx_http_header_filter_module.c、还有一个ngx_http_special_response.c。提示:一般修改都是在nginx编译之前修改,修改完了之后需要重新编译代码如下:scr/core/nginx.conf#definenginx_version"1.4.7"#definenginx_ver"nginx/"n

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools