<?php

 $host = 'ldap://localhost:62222';
 $baseDn = 'c=KZ';
 $userfield = 'userCertificate';
 $pid = '*';
 $credential = "";

 $ldap = ldap_connect($host);
 ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
 if (@ldap_bind($ldap, $baseDn, $credential)) {}
 else
 {
    print('ldap_bind error');
 }
 if (0) {}
 else if (isset($ldap) && $ldap != '')
 {
    $result = @ldap_search($ldap, $baseDn, $userfield.'='.$pid, array('dn:t=L;','cn'));
    if ($result != 0)
    {
        echo "Number of entires returned is " . ldap_count_entries($ldap, $result) . "\n";
        $entries = ldap_get_entries($ldap, $result);

        for ($i=0; $i<$entries["count"]; $i++)
        {
            echo "N" . $i . " -> DN is: " . $entries[$i]["dn"] . "\n";
        }
        $principal = $entries[0]['cn'];
        if (isset($principal))
        {
            print('OK...');
        }
        else
        {
            print('User not found in LDAP');
        }
        ldap_free_result($result);
   }
   else
   {
        print('Error occured searching the LDAP');
   }
   ldap_close($ldap);
 }
 else
 {
   print('Could not connect to LDAP at '.$host);
 }

?>