<!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>Compare value of attribute found in entry specified with DN</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ldap-close.html">ldap_close</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.ldap-connect.html">ldap_connect</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-compare" class="refentry"> <div class="refnamediv"> <h1 class="refname">ldap_compare</h1> <p class="verinfo">(PHP 4 >= 4.0.2, PHP 5)</p><p class="refpurpose"><span class="refname">ldap_compare</span> — <span class="dc-title">Compare value of attribute found in entry specified with DN</span></p> </div> <div class="refsect1 description" id="refsect1-function.ldap-compare-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>ldap_compare</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$link_identifier</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$dn</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$attribute</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$value</code></span> )</div> <p class="para rdfs-comment"> Compare <em><code class="parameter">value</code></em> of <em><code class="parameter">attribute</code></em> with value of same attribute in an LDAP directory entry. </p> </div> <div class="refsect1 parameters" id="refsect1-function.ldap-compare-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">link_identifier</code></em></span> <dd> <p class="para"> An LDAP link identifier, returned by <span class="function"><a href="function.ldap-connect.html" class="function">ldap_connect()</a></span>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">dn</code></em></span> <dd> <p class="para"> The distinguished name of an LDAP entity. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">attribute</code></em></span> <dd> <p class="para"> The attribute name. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">value</code></em></span> <dd> <p class="para"> The compared value. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.ldap-compare-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> if <em><code class="parameter">value</code></em> matches otherwise returns <strong><code>FALSE</code></strong>. Returns -1 on error. </p> </div> <div class="refsect1 examples" id="refsect1-function.ldap-compare-examples"> <h3 class="title">Examples</h3> <p class="para"> The following example demonstrates how to check whether or not given password matches the one defined in DN specified entry. </p> <p class="para"> <div class="example" id="example-4390"> <p><strong>Example #1 Complete example of password check</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$ds</span><span style="color: #007700">=</span><span style="color: #0000BB">ldap_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">); </span><span style="color: #FF8000">// assuming the LDAP server is on this host<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$ds</span><span style="color: #007700">) {<br /><br /> </span><span style="color: #FF8000">// bind<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">ldap_bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$ds</span><span style="color: #007700">)) {<br /><br /> </span><span style="color: #FF8000">// prepare data<br /> </span><span style="color: #0000BB">$dn </span><span style="color: #007700">= </span><span style="color: #DD0000">"cn=Matti Meikku, ou=My Unit, o=My Company, c=FI"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #DD0000">"secretpassword"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$attr </span><span style="color: #007700">= </span><span style="color: #DD0000">"password"</span><span style="color: #007700">;<br /><br /> </span><span style="color: #FF8000">// compare value<br /> </span><span style="color: #0000BB">$r</span><span style="color: #007700">=</span><span style="color: #0000BB">ldap_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">$ds</span><span style="color: #007700">, </span><span style="color: #0000BB">$dn</span><span style="color: #007700">, </span><span style="color: #0000BB">$attr</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br /><br /> if (</span><span style="color: #0000BB">$r </span><span style="color: #007700">=== -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Error: " </span><span style="color: #007700">. </span><span style="color: #0000BB">ldap_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$ds</span><span style="color: #007700">);<br /> } elseif (</span><span style="color: #0000BB">$r </span><span style="color: #007700">=== </span><span style="color: #0000BB">true</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Password correct."</span><span style="color: #007700">;<br /> } elseif (</span><span style="color: #0000BB">$r </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Wrong guess! Password incorrect."</span><span style="color: #007700">;<br /> }<br /><br /> } else {<br /> echo </span><span style="color: #DD0000">"Unable to bind to LDAP server."</span><span style="color: #007700">;<br /> }<br /><br /> </span><span style="color: #0000BB">ldap_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ds</span><span style="color: #007700">);<br /><br />} else {<br /> echo </span><span style="color: #DD0000">"Unable to connect to LDAP server."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.ldap-compare-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> <span class="function"><strong>ldap_compare()</strong></span> can NOT be used to compare BINARY values! </p> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ldap-close.html">ldap_close</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.ldap-connect.html">ldap_connect</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>