Connect to the LDAP server using PHP (continued)

Source: Internet
Author: User
In our example, $ format_array is our new array, which contains the query results and is formatted for output. First, cycle every element in $ result_array and assign it to a two-dimensional array for sorting. At the same time, we use the strtolower () function to convert all values to lowercase letters. Next, we use PHP with LDAP

In our example, $ format_array is our new array, which contains the query results and is formatted for output. First, cycle every element in $ result_array and assign it to a two-dimensional array for sorting. At the same time, we use the strtolower () function to convert all values to lowercase letters.



Next, we use a function called sort () that comes with PHP to sort data. The first parameter is the array to be sorted, and the other is the sort type to be executed, which is defined by the PHP document. Because we sort strings, we use "SORT_STRING ".



Third, we loop through the formatted array and allocate it to an output character named $ result_list, which contains the HTML description. Note that the URL format of ldap is used in the hyperlink. The format example is similar to: HREF = "ldap: // ldap.domain.net/uid?dannie,dc=domain.net ".



Close connection



Now all our data is included in $ result_list. we can safely close the LDAP connection.



<? Php



// Close the connection

Ldap_close ($ connect_id );





Customize the HTML table on the search page



Finally, we need to customize the HTML table used for search, which is used to perform search for users.





// Customize the table

Echo "<CENTER> <FORM action = '" $ PHP_SELF "'method ='" GET "'> ";

Echo "Search in: <SELECT name = '" SERVER_ID "'>"; // loop to create the SELECT option for ($ I = 0; $ I <COUNT ($ LDAP_NAME ); <br $ I ++)> echo "<OPTION selected value = '" $ I "'> ". $ LDAP_NAME [$ I]. "</OPTION>"; echo "</SELECT>

";

Echo "Search for: <INPUT name = '" common "'type ='" text "'> ";

Echo "<INPUT name = '" lookup "'type ='" submit "'value = '" go "'>

";

Echo "(You can use * for wildcard searches, ex. * Stanley will find all Stanleys)

";

Echo "</FORM> </CENTER> ";



?>



In the code, $ PHP_SELF is a global constant representing the script page itself. The loop is used to create the SELECT option through our $ LDAP_NAME variable.



Display result



Now that all the work has been completed, we will print the result set. If No matching result is displayed, "No Results" is displayed.



<?> Php



// Display the result

If ($ result_list)

{

Echo "<CENTER> <TABLE border = '" 1 "'cellpadding ='" 10 "'cellspacing = '" 0 "'

BGCOLOR = "# FFFFEA" WIDTH = "450"> <TBODY> <TR> <TD> $ result_list </TD> </TR>

</TBODY> </TABLE> </CENTER> ";

}

Else

Echo "No Results ";





?>



Source code



The following is the complete source code. you just need to cut and paste it into an HTML document and try it.



<? Php



$ LDAP_NAME [0] = "Netscape Net Center ";

$ LDAP_SERVER [0] = "memberdir.netscape.com ";

$ LDAP_ROOT_DN [0] = "ou = member_directory, o = netcenter.com ";



$ LDAP_NAME [1] = "Bigfoot ";

$ LDAP_SERVER [1] = "ldap.bigfoot.com ";

$ LDAP_ROOT_DN [1] = "";



// Set it to 0 if no server is selected

If (! $ SERVER_ID)

$ SERVER_ID = 0;



// Create a query

$ Ldap_query = "cn = $ common ";



// Connect to LDAP

$ Connect_id = ldap_connect ($ LDAP_SERVER [$ SERVER_ID]);



If ($ connect_id)

{

// Authentication

$ Bind_id = ldap_bind ($ connect_id );



// Execute the search

$ Search_id = ldap_search ($ connect_id, $ LDAP_ROOT_DN [$ SERVER_ID], $ ldap_query );



// Assign the result set to an array

$ Result_array = ldap_get_entries ($ connect_id, $ search_id );

}

Else

{

// Display connection error

Echo "cocould not connect to LDAP server: $ LDAP_SERVER [$ SERVER_ID]";

}



// If the search is successful, sort the results

If ($ result_array)

{

For ($ I = 0; $ I {

$ Format_array [$ I] [0] = strtolower ($ result_array [$ I] ["cn"] [0]);

$ Format_array [$ I] [1] = $ result_array [$ I] ["dn"];

$ Format_array [$ I] [2] = strtolower ($ result_array [$ I] ["givenname"] [0]);

$ Format_array [$ I] [3] = strtolower ($ result_array [$ I] ["sn"] [0]);

$ Format_array [$ I] [4] = strtolower ($ result_array [$ I] ["mail"] [0]);

}



// Sort the array

Sort ($ format_array, "SORT_STRING ");



For ($ I = 0; $ I {

$ Cn = $ format_array [$ I] [0];

$ Dn = $ format_array [$ I] [1];

$ Fname = ucwords ($ format_array [$ I] [2]);

$ Lname = ucwords ($ format_array [$ I] [3]);

$ Email = $ format_array [$ I] [4];



If ($ dn & $ fname & $ lname & $ email)

{

$ Result_list. = "<A href = '/" ldap: // $ LDAP_SERVER [$ SERVER_ID]/$ dn/"'> $ fname $ lname </A> ";

$ Result_list. = "<$ email>

";

}

Elseif ($ dn & $ cn & $ email)

{

$ Result_list. = "<A href = '/" ldap: // $ LDAP_SERVER [$ SERVER_ID]/$ dn/"'> $ cn </A> ";

$ Result_list. = "<A href = '/" mailto: $ email/"'> $ email </A>

";

}

}

}

Else

{

Echo "Result set empty for query: $ ldap_query ";

}



// Close the connection

Ldap_close ($ connect_id );



// Customize the table

Echo "<CENTER> <FORM action = '" $ PHP_SELF "'method ='" GET "'> ";

Echo "Search in: <SELECT name = '" SERVER_ID "'>"; // loop to create the SELECT option for ($ I = 0; $ I echo "<OPTION selected value = '" $ I "'> ". $ LDAP_NAME [$ I]. "</OPTION>"; echo "</SELECT>

";

Echo "Search for: <INPUT name = '" common "'type ='" text "'> ";

Echo "<INPUT name = '" lookup "'type ='" submit "'value = '" go "'>

";

Echo "(You can use * for wildcard searches, ex. * Stanley will find all Stanleys)

";

Echo "</FORM> </CENTER> ";



// Display the result

If ($ result_list)

{

Echo "<CENTER> <TABLE border = '" 1 "'cellpadding ='" 10 "'cellspacing = '" 0 "'

BGCOLOR = "# FFFFEA" WIDTH = "450"> <TBODY> <TR> <TD> $ result_list </TD> </TR>

</TBODY> </TABLE> </CENTER> ";

}

Else

Echo "No Results ";

}



?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.