在PHP中使用LDAP(輕量目錄訪問協議)可以實現對目錄服務器的連接、搜索、添加、修改和刪除等操作。以下是使用LDAP的基本方法:
$ldapServer = "ldap://ldap.example.com";
$ldapPort = 389;
$ldapConn = ldap_connect($ldapServer, $ldapPort);
$ldapBindUser = "cn=admin,dc=example,dc=com";
$ldapBindPass = "password";
ldap_bind($ldapConn, $ldapBindUser, $ldapBindPass);
$ldapBaseDn = "dc=example,dc=com";
$ldapFilter = "(objectClass=*)";
$ldapAttributes = array("cn", "mail");
$ldapSearch = ldap_search($ldapConn, $ldapBaseDn, $ldapFilter, $ldapAttributes);
$ldapEntries = ldap_get_entries($ldapConn, $ldapSearch);
$ldapDn = "cn=John Doe,ou=Users,dc=example,dc=com";
$ldapEntry = array(
"cn" => "John Doe",
"sn" => "Doe",
"mail" => "john.doe@example.com"
);
ldap_add($ldapConn, $ldapDn, $ldapEntry);
$ldapDn = "cn=John Doe,ou=Users,dc=example,dc=com";
$ldapEntry = array("mail" => "john.doe.new@example.com");
ldap_modify($ldapConn, $ldapDn, $ldapEntry);
$ldapDn = "cn=John Doe,ou=Users,dc=example,dc=com";
ldap_delete($ldapConn, $ldapDn);
ldap_close($ldapConn);
以上是基本的LDAP操作方法,根據具體需求可以進一步擴展和優化。在使用LDAP時,請確保已啟用PHP的LDAP擴展并配置好LDAP服務器的訪問權限和連接信息。