php使用ldap
………………………………………………………..
環境預設如下
ldap主機執行bind_anon停用匿名繫結
ldap主機為10.20.30.40
ldap主機使用的port為389
驗證用的dn為uid=raymond,ou=people,dc=study,dc=net
驗證用的dn密碼為1234567
要顯示相關資料的user是raymond
第一種情況
1
dn結構相同如下
uid=raymond,ou=people,dc=study,dc=net
uid=tom,ou=people,dc=study,dc=net
uid=peter,ou=people,dc=study,dc=net
可用以下程式碼解決
//ldap相關參數
$ldap[‘host’] = ‘10.20.30.40’;
$ldap[‘port’] = 389;
$ldap[‘user’] = ‘raymond’;
$ldap[‘dn’] = ‘uid=’.$ldap[‘user’].’,ou=people,dc=study,dc=net’;
$ldap[‘dnpass’] = ‘1234567’;
$ldap[‘base’] = ‘ou=people,dc=study,dc=net’;
//與ldap連線
$ldapconnect=ldap_connect($ldap[‘host’],$ldap[‘port’])or die(‘Could not connect to server’);
//驗證ldap帳密
$ldapbind=ldap_bind($ldapconnect,$ldap[‘dn’],$ldap[‘dnpass’]);
if(!$ldapbind){echo ldap_error($ldapconnect);exit;}
//到此認證階段以結束,以下是選擇性code
//取得uid=$ldap[‘user’]的相關資訊
$ldapresult=ldap_search($ldapconnect,$ldap[‘base’],’uid=’.$ldap[‘user’]);
if($ldapresult){
//將$ldap[‘user’]的資訊存到$ldapinfo陣列
$ldapinfo=ldap_get_entries($ldapconnect,$ldapresult);
}
//顯示$ldapinfo陣列內資料
if($ldapinfo){
print_r($ldapinfo);
}
//關閉ldap連線
ldap_close($ldapconnect);
?>
第2種情況
1
dn結構不同,如下
uid=raymond,ou=sales,ou=taipei,dc=study,dc=net
uid=tom,ou=account,dc=study,dc=net
uid=peter,ou=technology,dc=study,dc=net
2
密碼儲存在userpassword欄位
可用以下程式碼儲存
$ldap['host'] = '10.20.30.40';
$ldap['port'] = 389;
$ldap['uid']='raymond';
$ldap['dnpw']='12345676';
$ldap['basedn']="dc=study,dc=net";
// connecting to ldap
$ldap['conn']=ldap_connect($ldap['host'],$ldap['port'])or die('Could not connect to server');
//search uid="raymond"
$filter="(&(uid=".$ldap['uid'].")(userpassword=*))";
$sr=ldap_search($ldap['conn'],$ldap['basedn'],$filter,array("uid"));
//get search result
$ldapinfo = ldap_get_entries($ldap['conn'],$sr);
$ldap['dn']=$ldapinfo[0]['dn'];
// binding to ldap
$ldap['bind']=@ldap_bind($ldap['conn'],$ldap['dn'],$ldap['dnpw']);
if($ldap['bind']){echo 'auth ok'; }
……………………………………………………………………..
ldap_get_entries抓取的陣列格式
若ldap的格式為
dn: uid=raymond, ou=people, dc=study, dc=net
uid: raymond
cn: raymond
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowLastChange: 611773
loginShell: /usr/bin/tcsh
uidNumber: 1178
gidNumber: 10
homeDirectory: /home/raymond
userPassword:: e2NyeXB0fVdQZWt5T3hiYzYzY1U=
則使用ldap_get_entries抓出來的資料會如下
Array (
[count] => 1
[0] => Array (
[dn] => uid=raymond2, ou=people, dc=study, dc=net )
[count] => 9
[0] => uid
[uid] => Array (
[count] => 1
[0] => raymond )
[1] => cn
[cn] => Array (
[count] => 1
[0] => raymond )
[2] => objectclass
[objectclass] => Array (
[count] => 4
[0] => account
[1] => posixAccount
[2] => top
[3] => shadowAccount )
[3] => shadowlastchange
[shadowlastchange] => Array (
[count] => 1
[0] => 611773 )
[4] => loginshell
[loginshell] => Array (
[count] => 1
[0] => /usr/bin/tcsh )
[5] => uidnumber
[uidnumber] => Array (
[count] => 1
[0] => 1178 )
[6] => gidnumber
[gidnumber] => Array (
[count] => 1
[0] => 10 )
[7] => homedirectory
[homedirectory] => Array (
[count] => 1
[0] => /home/raymond )
[8] => userpassword
[userpassword] => Array (
[count] => 1
[0] => {crypt}WPekyOxbc63cU )