<HTML ><HEAD ><TITLE >LDAP functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Function Reference" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="java_last_exception_get" HREF="function.java-last-exception-get.html"><LINK REL="NEXT" TITLE="ldap_8859_to_t61" HREF="function.ldap-8859-to-t61.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="reference" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.java-last-exception-get.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.ldap-8859-to-t61.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.ldap" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >XLVIII. LDAP functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN42402" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="ldap.intro" ></A >Introduction</H1 ><P > LDAP is the Lightweight Directory Access Protocol, and is a protocol used to access "Directory Servers". The Directory is a special kind of database that holds information in a tree structure. </P ><P > The concept is similar to your hard disk directory structure, except that in this context, the root directory is "The world" and the first level subdirectories are "countries". Lower levels of the directory structure contain entries for companies, organisations or places, while yet lower still we find directory entries for people, and perhaps equipment or documents. </P ><P > To refer to a file in a subdirectory on your hard disk, you might use something like: </P ><P CLASS="literallayout" ><br> /usr/local/myapp/docs<br> </P ><P > The forwards slash marks each division in the reference, and the sequence is read from left to right. </P ><P > The equivalent to the fully qualified file reference in LDAP is the "distinguished name", referred to simply as "dn". An example dn might be: </P ><P CLASS="literallayout" ><br> cn=John Smith,ou=Accounts,o=My Company,c=US<br> </P ><P > The comma marks each division in the reference, and the sequence is read from right to left. You would read this dn as: </P ><P CLASS="literallayout" ><br> country = US<br> organization = My Company<br> organizationalUnit = Accounts<br> commonName = John Smith<br> </P ><P > In the same way as there are no hard rules about how you organise the directory structure of a hard disk, a directory server manager can set up any structure that is meaningful for the purpose. However, there are some conventions that are used. The message is that you can not write code to access a directory server unless you know something about its structure, any more than you can use a database without some knowledge of what is available. </P ><P > Lots of information about LDAP can be found at </P ><P ></P ><UL ><LI ><P > <A HREF="http://developer.netscape.com/tech/directory/" TARGET="_top" >Netscape</A > </P ></LI ><LI ><P > <A HREF="http://www.umich.edu/~dirsvcs/ldap/index.html" TARGET="_top" >University of Michigan</A > </P ></LI ><LI ><P > <A HREF="http://www.openldap.org/" TARGET="_top" >OpenLDAP Project</A > </P ></LI ><LI ><P > <A HREF="http://www.innosoft.com/ldapworld" TARGET="_top" >LDAP World</A > </P ></LI ></UL ><P > The Netscape SDK contains a helpful Programmer's Guide in HTML format. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="ldap.requirements" ></A >Requirements</H1 ><P > You will need to get and compile LDAP client libraries from either the University of Michigan ldap-3.3 package or the Netscape Directory SDK 3.0 to compile PHP with LDAP support. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="ldap.installation" ></A >Installation</H1 ><P > LDAP support in PHP is not enabled by default. You will need to use the <TT CLASS="option" >--with-ldap[=DIR]</TT > configuration option when compiling PHP to enable LDAP support. DIR is the LDAP base install directory. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note to Win32 Users: </B > In order to enable this module on a Windows environment, you must copy <SPAN CLASS="emphasis" ><I CLASS="emphasis" >libsasl.dll</I ></SPAN > from the DLL folder of the PHP/Win32 binary package to the SYSTEM32 folder of your windows machine. (Ex: C:\WINNT\SYSTEM32 or C:\WINDOWS\SYSTEM32) </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="ldap.configuration" ></A >Runtime Configuration</H1 ><P > The behaviour of these functions is affected by settings in <TT CLASS="filename" >php.ini</TT >. </P ><P > <DIV CLASS="table" ><A NAME="AEN42446" ></A ><P ><B >Table 1. LDAP configuration options</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Name</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Default</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Changeable</TH ></TR ></THEAD ><TBODY ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >ldap.max_links</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"-1"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ></TBODY ></TABLE ></DIV > For further details and definition of the PHP_INI_* constants see <A HREF="function.ini-set.html" ><B CLASS="function" >ini_set()</B ></A >. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="ldap.resources" ></A >Resource Types</H1 ><P >This extension has no resource types defined.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="ldap.constants" ></A >Predefined Constants</H1 ><P > The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime. </P ><P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="constant" ><B >LDAP_DEREF_NEVER</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_DEREF_SEARCHING</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_DEREF_FINDING</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_DEREF_ALWAYS</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_DEREF</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_SIZELIMIT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_TIMELIMIT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_PROTOCOL_VERSION</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_ERROR_NUMBER</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_REFERRALS</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_RESTART</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_HOST_NAME</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_ERROR_STRING</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_MATCHED_DN</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_SERVER_CONTROLS</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_CLIENT_CONTROLS</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >LDAP_OPT_DEBUG_LEVEL</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >GSLC_SSL_NO_AUTH</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >GSLC_SSL_ONEWAY_AUTH</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >GSLC_SSL_TWOWAY_AUTH</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="ldap.examples" ></A >Examples</H1 ><P > Retrieve information for all entries where the surname starts with "S" from a directory server, displaying an extract with name and email address. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN42590" ></A ><P ><B >Example 1. LDAP search example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // basic sequence with LDAP is connect, bind, search, interpret search // result, close connection echo "<h3>LDAP query test</h3>"; echo "Connecting ..."; $ds=ldap_connect("localhost"); // must be a valid LDAP server! echo "connect result is ".$ds."<p>"; if ($ds) { echo "Binding ..."; $r=ldap_bind($ds); // this is an "anonymous" bind, typically // read-only access echo "Bind result is ".$r."<p>"; echo "Searching for (sn=S*) ..."; // Search surname entry $sr=ldap_search($ds,"o=My Company, c=US", "sn=S*"); echo "Search result is ".$sr."<p>"; echo "Number of entires returned is ".ldap_count_entries($ds,$sr)."<p>"; echo "Getting entries ...<p>"; $info = ldap_get_entries($ds, $sr); echo "Data for ".$info["count"]." items returned:<p>"; for ($i=0; $i<$info["count"]; $i++) { echo "dn is: ". $info[$i]["dn"] ."<br>"; echo "first cn entry is: ". $info[$i]["cn"][0] ."<br>"; echo "first email entry is: ". $info[$i]["mail"][0] ."<p>"; } echo "Closing connection"; ldap_close($ds); } else { echo "<h4>Unable to connect to LDAP server</h4>"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="ldap.using" ></A >Using the PHP LDAP calls</H1 ><P > Before you can use the LDAP calls you will need to know .. <P ></P ><UL ><LI ><P > The name or address of the directory server you will use </P ></LI ><LI ><P > The "base dn" of the server (the part of the world directory that is held on this server, which could be "o=My Company,c=US") </P ></LI ><LI ><P > Whether you need a password to access the server (many servers will provide read access for an "anonymous bind" but require a password for anything else) </P ></LI ></UL ></P ><P > The typical sequence of LDAP calls you will make in an application will follow this pattern: <P CLASS="literallayout" ><br> ldap_connect() // establish connection to server<br> |<br> ldap_bind() // anonymous or authenticated "login"<br> |<br> do something like search or update the directory<br> and display the results<br> |<br> ldap_close() // "logout"<br> </P ></P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="function.ldap-8859-to-t61.html" >ldap_8859_to_t61</A > -- Translate 8859 characters to t61 characters </DT ><DT ><A HREF="function.ldap-add.html" >ldap_add</A > -- Add entries to LDAP directory</DT ><DT ><A HREF="function.ldap-bind.html" >ldap_bind</A > -- Bind to LDAP directory</DT ><DT ><A HREF="function.ldap-close.html" >ldap_close</A > -- Close link to LDAP server</DT ><DT ><A HREF="function.ldap-compare.html" >ldap_compare</A > -- Compare value of attribute found in entry specified with DN</DT ><DT ><A HREF="function.ldap-connect.html" >ldap_connect</A > -- Connect to an LDAP server</DT ><DT ><A HREF="function.ldap-count-entries.html" >ldap_count_entries</A > -- Count the number of entries in a search</DT ><DT ><A HREF="function.ldap-delete.html" >ldap_delete</A > -- Delete an entry from a directory</DT ><DT ><A HREF="function.ldap-dn2ufn.html" >ldap_dn2ufn</A > -- Convert DN to User Friendly Naming format</DT ><DT ><A HREF="function.ldap-err2str.html" >ldap_err2str</A > -- Convert LDAP error number into string error message </DT ><DT ><A HREF="function.ldap-errno.html" >ldap_errno</A > -- Return the LDAP error number of the last LDAP command </DT ><DT ><A HREF="function.ldap-error.html" >ldap_error</A > -- Return the LDAP error message of the last LDAP command </DT ><DT ><A HREF="function.ldap-explode-dn.html" >ldap_explode_dn</A > -- Splits DN into its component parts</DT ><DT ><A HREF="function.ldap-first-attribute.html" >ldap_first_attribute</A > -- Return first attribute</DT ><DT ><A HREF="function.ldap-first-entry.html" >ldap_first_entry</A > -- Return first result id</DT ><DT ><A HREF="function.ldap-first-reference.html" >ldap_first_reference</A > -- Return first reference </DT ><DT ><A HREF="function.ldap-free-result.html" >ldap_free_result</A > -- Free result memory</DT ><DT ><A HREF="function.ldap-get-attributes.html" >ldap_get_attributes</A > -- Get attributes from a search result entry</DT ><DT ><A HREF="function.ldap-get-dn.html" >ldap_get_dn</A > -- Get the DN of a result entry</DT ><DT ><A HREF="function.ldap-get-entries.html" >ldap_get_entries</A > -- Get all result entries</DT ><DT ><A HREF="function.ldap-get-option.html" >ldap_get_option</A > -- Get the current value for given option</DT ><DT ><A HREF="function.ldap-get-values-len.html" >ldap_get_values_len</A > -- Get all binary values from a result entry</DT ><DT ><A HREF="function.ldap-get-values.html" >ldap_get_values</A > -- Get all values from a result entry</DT ><DT ><A HREF="function.ldap-list.html" >ldap_list</A > -- Single-level search</DT ><DT ><A HREF="function.ldap-mod-add.html" >ldap_mod_add</A > -- Add attribute values to current attributes</DT ><DT ><A HREF="function.ldap-mod-del.html" >ldap_mod_del</A > -- Delete attribute values from current attributes</DT ><DT ><A HREF="function.ldap-mod-replace.html" >ldap_mod_replace</A > -- Replace attribute values with new ones</DT ><DT ><A HREF="function.ldap-modify.html" >ldap_modify</A > -- Modify an LDAP entry</DT ><DT ><A HREF="function.ldap-next-attribute.html" >ldap_next_attribute</A > -- Get the next attribute in result</DT ><DT ><A HREF="function.ldap-next-entry.html" >ldap_next_entry</A > -- Get next result entry</DT ><DT ><A HREF="function.ldap-next-reference.html" >ldap_next_reference</A > -- Get next reference </DT ><DT ><A HREF="function.ldap-parse-reference.html" >ldap_parse_reference</A > -- Extract information from reference entry </DT ><DT ><A HREF="function.ldap-parse-result.html" >ldap_parse_result</A > -- Extract information from result </DT ><DT ><A HREF="function.ldap-read.html" >ldap_read</A > -- Read an entry</DT ><DT ><A HREF="function.ldap-rename.html" >ldap_rename</A > -- Modify the name of an entry</DT ><DT ><A HREF="function.ldap-search.html" >ldap_search</A > -- Search LDAP tree</DT ><DT ><A HREF="function.ldap-set-option.html" >ldap_set_option</A > -- Set the value of the given option</DT ><DT ><A HREF="function.ldap-set-rebind-proc.html" >ldap_set_rebind_proc</A > -- Set a callback function to do re-binds on referral chasing. </DT ><DT ><A HREF="function.ldap-sort.html" >ldap_sort</A > -- Sort LDAP result entries </DT ><DT ><A HREF="function.ldap-start-tls.html" >ldap_start_tls</A > -- Start TLS </DT ><DT ><A HREF="function.ldap-t61-to-8859.html" >ldap_t61_to_8859</A > -- Translate t61 characters to 8859 characters </DT ><DT ><A HREF="function.ldap-unbind.html" >ldap_unbind</A > -- Unbind from LDAP directory</DT ></DL ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.java-last-exception-get.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.ldap-8859-to-t61.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >java_last_exception_get</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="funcref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >ldap_8859_to_t61</TD ></TR ></TABLE ></DIV ></BODY ></HTML >