|
|
|
รบกวนอธิบายการใช้งาน Function: ldap_search() 1. ผมไม่เข้าใจว่าต้องใส่ข้อมูลอะไรลงไปบ้างครับ |
|
|
|
|
|
|
|
<?php
// Set some Variables
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
// Bind to LDAP Server // Binding Anonymously
$ldapusername = "xxxxxxxx"; // LDAP RDN or DN
$ldappwd = "xxxxxxxxx"; // Associated Password
$ldapbind = ldap_bind($ldapconn ,$ldapusername, $ldappwd) or die ("ระบบไม่สามารถเชื่อมต่อ LDAP Server ได้ !!!");
// ค้นหาพนักงานที่มีอีเมล์ และรหัสพนักงาน
$filter = "(&(mail=*)(pager=*))"; // CN = Common Name, SN = Surname , OU = Organization Unit
// Search surname entry
$result = ldap_search($ldapconn,"DC=abc,DC=com",$filter);
echo "Search result is ".$result ."<br>";
echo "Number of entires returned is ".ldap_count_entries($ldapconn,$result )."<p>";
$info = ldap_get_entries($ldapconn, $result );
for ($i=0; $i<$info["count"]; $i++){
// กำหนดค่าตัวเปร
$userNameID = $info[$i]["pager"][0];
$userAccount = $info[$i]["samaccountname"][0];
$firstName = $info[$i]["givenname"][0];
$sureName = $info[$i]["sn"][0];
$fullName = $info[$i]["displayname"][0];
$eMail = $info[$i]["mail"][0];
$position = $info[$i]["title"][0];
$location = $info[$i]["physicaldeliveryofficename"][0];
$tel = $info[$i]["telephonenumber"][0];
echo "<option value=\"". $info[$i]["displayname"][0] ."\">".$info[$i]["pager"][0]." ".$info[$i]["displayname"][0]."</option>";
}
?>
|
|
|
|
|
Date :
2010-04-26 16:22:14 |
By :
pukmtec |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จากโค๊ดข้างบนนะครับ มาดัดแปลงเล็กน้อยตาม Code ข้างล่าง
Code (PHP)
<?php
$dn_base = 'DC=company,DC=co,DC=th';
$dn_host = 'ldap://localhost/';
$ldapusers = '[email protected]';
$ldappasswd = '30fri53';
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connect.
echo "Connecting to LDAP Server.. <br/>";
$ldapconn = ldap_connect("localhost", 389)
or die("Could not connect to LDAP Server.");
echo "Connect status is ".$ldapconn."<br>";
// Set some Variables
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
// Bind to LDAP Server // Binding Anonymously
$ldapbind = ldap_bind($ldapconn ,$ldapusers, $ldappasswd) or die ("Users or Password Invalid.");
echo "Connect Authentication is: ".$ldapbind."<p>";
// Search
$filter = "(&(mail=*)(pager=*))"; // CN = Common Name, SN = Surname , OU = Organization Unit
// Search surname entry
$result = ldap_search($ldapconn,$dn_base,$filter);
echo "Search result is ".$result ."<br>";
$entries = ldap_count_entries($ldapconn,$result );
echo "Number of entires returned is:".$entries."<p>";
$info = ldap_get_entries($ldapconn, $result );
for ($i=0; $i<$info["count"]; $i++){
// Variable
$userNameID = $info[$i]["pager"][0];
$userAccount = $info[$i]["samaccountname"][0];
$firstName = $info[$i]["givenname"][0];
$sureName = $info[$i]["sn"][0];
$fullName = $info[$i]["displayname"][0];
$eMail = $info[$i]["mail"][0];
$position = $info[$i]["title"][0];
$location = $info[$i]["physicaldeliveryofficename"][0];
$tel = $info[$i]["telephonenumber"][0];
echo "<option value=\"". $info[$i]["displayname"][0] ."\">".$info[$i]["pager"][0]." ".$info[$i]["displayname"][0]."</option>";
}
?>
ได้ผลตามนี้ครับ
Connecting to LDAP Server..
Connect status is Resource id #2
Connect Authentication is: 1
Search result is Resource id #3
Number of entires returned is:0
|
|
|
|
|
Date :
2010-04-28 14:18:32 |
By :
alteisen |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|