«^»
4.4. Accessing an LDAP server

LDAP (Lightweight Directory Access Protocol) provides a way of storing data such as Directory information (or White Page information).

The University of Durham's LDAP server enables anyone who has access to the WWW to find out the e-mail address and phone number of members of the University. Here is a WWW form that asks the visitor to the WWW page to type in part of a person's name:

0114: <HTML><BODY> 
0115: <FORM METHOD="POST" ACTION="ldap.php"> 
0116:    To search for someone from an LDAP server,
0117:    type in the name of the server:
0118:    <INPUT TYPE="text" NAME="server">
0119:    <P>
0120:    type in the search base:
0121:    <INPUT TYPE="text" NAME="search_base">
0122:    <P>
0123:    type in part of their surname:
0124:    <INPUT TYPE="text" NAME="part_name">
0125:    <P> 
0126:    <INPUT TYPE="submit" VALUE="Run query"> 
0127: </FORM> 
0128: </BODY></HTML> 

And here is some PHP code to access the LDAP server. It uses six functions:

Here is the code of the ldap.php script:

0129: <HTML><BODY>
0130: <?php
0131:    $server = $_POST["server"];
0132:    $search_base = $_POST["search_base"];
0133:    $part_name = $_POST["part_name"];
0134:    $c_result = ldap_connect("$server");
0135:    $b_result = ldap_bind($c_result);
0136:    $s_result = ldap_search($c_result, "$search_base", "cn=*$part_name*");
0137:    $info = ldap_get_entries($c_result, $s_result);
0138:    $numrows = $info["count"];
0139:    if ( $numrows == 0 ) {
0140:       echo "<P>There is no entry with a name of $part_name</P>";
0141:       echo "</BODY></HTML>";
0142:       ldap_close($c_result);
0143:       exit;
0144:    }
0145: ?> 
0146:    <TABLE BORDER="1">
0147: <?php
0148:    for ($rownum = 0; $rownum<$numrows; $rownum++) {
0149: ?>
0150:       <TR>
0151:          <TD>
0152:             <?php echo $info[$rownum]["cn"][0]; ?>
0153:          </TD>
0154:          <TD>
0155:             <?php echo $info[$rownum]["ou"][0]; ?>
0156:          </TD>
0157:          <TD>
0158:             <?php echo $info[$rownum]["telephonenumber"][0]; ?>
0159:          </TD>
0160:          <TD>
0161:             <?php 
0162:                echo "<A HREF=mailto:"; 
0163:                echo $info[$rownum]["mail"][0];
0164:                echo ">";
0165:                echo $info[$rownum]["mail"][0];
0166:                echo "</A><BR>";
0167:             ?>
0168:          </TD>
0169:       </TR>
0170: <?php
0171:    }
0172: ?> 
0173:    </TABLE>
0174: <?php
0175:    ldap_close($c_result);
0176: ?>
0177: </BODY></HTML>

Go to the WWW form at: http://www.dur.ac.uk/barry.cornelius/papers/phpintro/code/ldap.htm