<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Connect to an LDAP server</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ldap-compare.html">ldap_compare</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.ldap-control-paged-result-response.html">ldap_control_paged_result_response</a></div> <div class="up"><a href="ref.ldap.html">LDAP Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.ldap-connect" class="refentry"> <div class="refnamediv"> <h1 class="refname">ldap_connect</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">ldap_connect</span> — <span class="dc-title">Connect to an LDAP server</span></p> </div> <div class="refsect1 description" id="refsect1-function.ldap-connect-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>ldap_connect</strong></span> ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$hostname</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = 389</span></span> ]] )</div> <p class="para rdfs-comment"> Establishes a connection to a LDAP server on a specified <em><code class="parameter">hostname</code></em> and <em><code class="parameter">port</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.ldap-connect-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">hostname</code></em></span> <dd> <p class="para"> If you are using OpenLDAP 2.x.x you can specify a URL instead of the hostname. To use LDAP with SSL, compile OpenLDAP 2.x.x with SSL support, configure PHP with SSL, and set this parameter as <em>ldaps://hostname/</em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">port</code></em></span> <dd> <p class="para"> The port to connect to. Not used when using URLs. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.ldap-connect-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a positive LDAP link identifier on success, or <strong><code>FALSE</code></strong> on error. When OpenLDAP 2.x.x is used, <span class="function"><strong>ldap_connect()</strong></span> will always return a <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> as it does not actually connect but just initializes the connecting parameters. The actual connect happens with the next calls to ldap_* funcs, usually with <span class="function"><a href="function.ldap-bind.html" class="function">ldap_bind()</a></span>. </p> <p class="para"> If no arguments are specified then the link identifier of the already opened link will be returned. </p> </div> <div class="refsect1 changelog" id="refsect1-function.ldap-connect-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.0.4</td> <td> URL and SSL support was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.ldap-connect-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4391"> <p><strong>Example #1 Example of connecting to LDAP server.</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// LDAP variables<br /></span><span style="color: #0000BB">$ldaphost </span><span style="color: #007700">= </span><span style="color: #DD0000">"ldap.example.com"</span><span style="color: #007700">; </span><span style="color: #FF8000">// your ldap servers<br /></span><span style="color: #0000BB">$ldapport </span><span style="color: #007700">= </span><span style="color: #0000BB">389</span><span style="color: #007700">; </span><span style="color: #FF8000">// your ldap server's port number<br /><br />// Connecting to LDAP<br /></span><span style="color: #0000BB">$ldapconn </span><span style="color: #007700">= </span><span style="color: #0000BB">ldap_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$ldaphost</span><span style="color: #007700">, </span><span style="color: #0000BB">$ldapport</span><span style="color: #007700">)<br /> or die(</span><span style="color: #DD0000">"Could not connect to </span><span style="color: #0000BB">$ldaphost</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-4392"> <p><strong>Example #2 Example of connecting securely to LDAP server.</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// make sure your host is the correct one<br />// that you issued your secure certificate to<br /></span><span style="color: #0000BB">$ldaphost </span><span style="color: #007700">= </span><span style="color: #DD0000">"ldaps://ldap.example.com/"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Connecting to LDAP<br /></span><span style="color: #0000BB">$ldapconn </span><span style="color: #007700">= </span><span style="color: #0000BB">ldap_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$ldaphost</span><span style="color: #007700">)<br /> or die(</span><span style="color: #DD0000">"Could not connect to </span><span style="color: #007700">{</span><span style="color: #0000BB">$ldaphost</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.ldap-connect-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.ldap-bind.html" class="function" rel="rdfs-seeAlso">ldap_bind()</a> - Bind to LDAP directory</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ldap-compare.html">ldap_compare</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.ldap-control-paged-result-response.html">ldap_control_paged_result_response</a></div> <div class="up"><a href="ref.ldap.html">LDAP Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>