http://www.perlmonks.org?node_id=56537
Category: networking code
Author/Contact Info Joseph Harnish Big_Joe1008@yahoo.com
Description: This is a quick LDAP search I did. This gives web users to search our internal "Phone book". I figure when I was trying to throw this together I couldn't find any information on it so here it is for everyone. Thanks to Chromatic for giving me a fix for the Win32 problem with the entry's.
#!/usr/bin/perl
use CGI;
use Net::LDAP qw(:all);
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header;
if($q->param('looking') eq 'yes'){
     $ldap = Net::LDAP->new('ldapserver') or die "$@";
 

     $ldap->bind;    # an anonymous bind
     my $base = "c=us";
     my @Attrs = ();
    my $search = "(&";
    if($q->param('first') ne ''){
            $search = "$search (givenName=" . $q->param('first') . ")"
+;
    }
    if($q->param('last') ne ''){
            $search = "$search (sn=" . $q->param('last') . ")";
    }
    if($q->param('dept') ne ''){
            $search = "$search (Department=" . $q->param('dept') . ")"
+;
    }
    if($q->param('phone') ne ''){
            $search = "$search (telephoneNumber=" . $q->param('phone')
+ . ")";
    }
    if($q->param('fax') ne ''){
            $search = "$search (facsimileTelephoneNumber=" . $q->param
+('fax') . ")";
    }
    $search = $search . ")";
    
    $result = LDAPsearch($ldap, $search,\@Attrs);
    @entries = $result->entries;  
    
     $ldap->unbind;   # take down session
 
 
 }
 



print <<EODUMP;

     header goes here
     <form method="post" action="directory.pl">
      <input type=hidden name=looking value=yes>
        <table width="85%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td colspan="2"><font size="2">Use this form to find a emp
+loyee. 
              Enter as much information as you know.</font></td>
          </tr>
          <tr> 
            <td width="20%"><font size="2">first Name</font></td>
            <td width="80%"> 
              <input type="text" name="first" size="25">
            </td>
          </tr>
          <tr> 
            <td width="20%"><font size="2">Last Name</font></td>
            <td width="80%"> 
              <input type="text" name="last" size="25" value="$user">
            </td>
          </tr>
          <tr> 
            <td width="20%"><font size="2">Department Name</font></td>
            <td width="80%"> 
              <input type="text" name="dept" size="25">
            </td>
          </tr>
         <tr> 
            <td width="20%"><font size="2">Phone</font></td>
            <td width="80%"> 
              <input type="text" name="phone" size="25">
            </td>
          </tr>
          <tr> 
            <td width="20%"><font size="2">Fax</font></td>
            <td width="80%"> 
              <input type="text" name="fax" size="25">
            </td>
          </tr>
          <tr align="center"> 
            <td colspan="2"><input type=image src="images/go.gif" widt
+h="30" height="20" border=0 alt="submit button"></a></td>
          </tr>
        </table>
      </form>

EODUMP

PRINTRESULTS();

print <<EODUMP;  
   footer goes here
EODUMP

sub PRINTRESULTS
{
print "<table>";
foreach $entr (@entries)
    {
             $phone = $entr->get('telephoneNumber');
              $user = $entr->get('cn');
              $email = $entr->get('mail');
              $department = $entr->get('Department');
              $address = $entr->get('address');
            $address = $address->[0];
            $department = $department->[0];
             $phone = $phone->[0];
              $user = $user->[0];
               $email = $email->[0]; 
            $title = $entr->get('title');
            $title = $title->[0];
            $street = $entr->get('postalAddress');
            $street = $street->[0];
            
             print "<tr><td><a href=\"mailto:$email\">$user</a></td>\n
+";
             print "<td>$phone</td></tr>\n";
            print "<tr><td>$department, $title\n</td></tr>\n";
            print "<tr><td>$street</td>\n";
            print "<tr><td>&nbsp;</td></tr>\n";
        
    }
    print "</table>";
}

sub LDAPsearch
 {
          my ($ldap,$searchString,$attrs) = @_ ;
          my $result = $ldap->search (
                base    => "$base",
                scope   => "sub",
                filter  => "$searchString",
                attrs   =>  $attrs,
        sizelimit => $size_limit
                );
 }