Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

LDAP Searcher

by BigJoe (Curate)
on Feb 06, 2001 at 02:58 UTC ( #56537=sourcecode: print w/replies, xml ) Need Help??
Category: networking code
Author/Contact Info Joseph Harnish
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.
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="">
      <input type=hidden name=looking value=yes>
        <table width="85%" border="0" cellspacing="0" cellpadding="0">
            <td colspan="2"><font size="2">Use this form to find a emp
              Enter as much information as you know.</font></td>
            <td width="20%"><font size="2">first Name</font></td>
            <td width="80%"> 
              <input type="text" name="first" size="25">
            <td width="20%"><font size="2">Last Name</font></td>
            <td width="80%"> 
              <input type="text" name="last" size="25" value="$user">
            <td width="20%"><font size="2">Department Name</font></td>
            <td width="80%"> 
              <input type="text" name="dept" size="25">
            <td width="20%"><font size="2">Phone</font></td>
            <td width="80%"> 
              <input type="text" name="phone" size="25">
            <td width="20%"><font size="2">Fax</font></td>
            <td width="80%"> 
              <input type="text" name="fax" size="25">
          <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>



print <<EODUMP;  
   footer goes here

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
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://56537]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2017-02-20 13:26 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (296 votes). Check out past polls.