LDAP-Series-1-Chapter-3 - PHP-LDAP
if you want to use php with ldap. you have to add the extension lib.(ldap.so)
Go to the install file of PHP.
$ cd /opt/php-5.4.5/ext/ldap/ $ /usr/local/bin/phpize $ ./configure --with-php-config=/usr/local/bin/php-config --with-ldap=/usr/local/openldap $ make $ make install $ cd /usr/local/lib $ vi php.ini (<strong>extension-dir=/usr/local/lib extension=php_ldap.so</strong>) $ cp /usr/local/lib/php/ext/..../ldap.so /usr/local/lib/php_ldap.so
$ php -v
PHP Test file.
<?php
class m_ldap{
private $ldapHost = "127.0.0.1";
private $ldapPort = 389;
private $ds;
function m_ldap_con(){
$this->ds = ldap_connect($this->ldapHost,$this->ldapPort) or die("Could not connect to $this->ldapHost");
return true;
}
function m_ldap_bind($dn,$psw){
ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
if($this->ds){
$r = ldap_bind($this->ds,$dn,$psw) or die("Could not bind to $dn");
if($r){
return true;
}else{
return false;
}
}else{
return false;
}
}
function m_ldap_add($dn,$info){
$r=ldap_add($this->ds,$dn,$info);
if($r){
return true;
}else{
return false;
}
}
function m_ldap_modify($dn,$info){
$r=ldap_modify($this->ds,$dn,$info);
if($r){
return true;
}else{
return false;
}
}
function m_ldap_search($dn,$filter){
$ldapSearch = ldap_search($this->ds,$dn,$filter);
$ldapInfo = ldap_get_entries($this->ds,$ldapSearch);
return $ldapInfo;
}
}
?><?php
require_once("m_ldap.php");
$mdap=new m_ldap();
$mdap->m_ldap_con();
$dn = "cn=root,ou=SystemAdmin,dc=xxx,dc=org";
$psw= "xxxxxx";
$mdap->m_ldap_bind($dn,$psw) or die("cannot bind");
$sdn="ou=people,ou=iWeb,dc=weiwejia,dc=org";
$filter="(uid=*)";
$res=$mdap->m_ldap_search($sdn,$filter);
foreach( $res[0]["cn"] as $key=>$val){
print $key."\n";
print $val."\n";
}
?>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
C++高性能并发应用_C++如何开发性能关键应用
Java AI集成Deep Java Library_Java怎么集成AI模型部署
Golang后端API开发_Golang如何高效开发后端和API
Python异步并发改进_Python异步编程有哪些新改进
C++系统编程内存管理_C++系统编程怎么与Rust竞争内存安全
Java GraalVM原生镜像构建_Java怎么用GraalVM构建高效原生镜像
Python FastAPI异步API开发_Python怎么用FastAPI构建异步API
C++现代C++20/23/26特性_现代C++有哪些新标准特性如modules和coroutines
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号