<?xml version="1.0" encoding="windows-1252"?>
<node id="56537" title="LDAP Searcher" created="2001-02-05 21:58:00" updated="2005-08-11 12:05:04">
<type id="1748">
sourcecode</type>
<author id="11400">
BigJoe</author>
<data>
<field name="doctext">
&lt;code&gt;
#!/usr/bin/perl
use CGI;
use Net::LDAP qw(:all);
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q-&gt;header;
if($q-&gt;param('looking') eq 'yes'){
 	$ldap = Net::LDAP-&gt;new('ldapserver') or die "$@";
 

 	$ldap-&gt;bind;    # an anonymous bind
 	my $base = "c=us";
 	my @Attrs = ();
    my $search = "(&amp;";
	if($q-&gt;param('first') ne ''){
			$search = "$search (givenName=" . $q-&gt;param('first') . ")";
	}
	if($q-&gt;param('last') ne ''){
			$search = "$search (sn=" . $q-&gt;param('last') . ")";
	}
	if($q-&gt;param('dept') ne ''){
			$search = "$search (Department=" . $q-&gt;param('dept') . ")";
	}
	if($q-&gt;param('phone') ne ''){
			$search = "$search (telephoneNumber=" . $q-&gt;param('phone') . ")";
	}
	if($q-&gt;param('fax') ne ''){
			$search = "$search (facsimileTelephoneNumber=" . $q-&gt;param('fax') . ")";
	}
	$search = $search . ")";
	
	$result = LDAPsearch($ldap, $search,\@Attrs);
	@entries = $result-&gt;entries;  
	
	 $ldap-&gt;unbind;   # take down session
 
 
 }
 



print &lt;&lt;EODUMP;

     header goes here
     &lt;form method="post" action="directory.pl"&gt;
	  &lt;input type=hidden name=looking value=yes&gt;
        &lt;table width="85%" border="0" cellspacing="0" cellpadding="0"&gt;
          &lt;tr&gt; 
            &lt;td colspan="2"&gt;&lt;font size="2"&gt;Use this form to find a employee. 
              Enter as much information as you know.&lt;/font&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt; 
            &lt;td width="20%"&gt;&lt;font size="2"&gt;first Name&lt;/font&gt;&lt;/td&gt;
            &lt;td width="80%"&gt; 
              &lt;input type="text" name="first" size="25"&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
		  &lt;tr&gt; 
            &lt;td width="20%"&gt;&lt;font size="2"&gt;Last Name&lt;/font&gt;&lt;/td&gt;
            &lt;td width="80%"&gt; 
              &lt;input type="text" name="last" size="25" value="$user"&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt; 
            &lt;td width="20%"&gt;&lt;font size="2"&gt;Department Name&lt;/font&gt;&lt;/td&gt;
            &lt;td width="80%"&gt; 
              &lt;input type="text" name="dept" size="25"&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
		 &lt;tr&gt; 
            &lt;td width="20%"&gt;&lt;font size="2"&gt;Phone&lt;/font&gt;&lt;/td&gt;
            &lt;td width="80%"&gt; 
              &lt;input type="text" name="phone" size="25"&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
		  &lt;tr&gt; 
            &lt;td width="20%"&gt;&lt;font size="2"&gt;Fax&lt;/font&gt;&lt;/td&gt;
            &lt;td width="80%"&gt; 
              &lt;input type="text" name="fax" size="25"&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
		  &lt;tr align="center"&gt; 
            &lt;td colspan="2"&gt;&lt;input type=image src="images/go.gif" width="30" height="20" border=0 alt="submit button"&gt;&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;
      &lt;/form&gt;

EODUMP

PRINTRESULTS();

print &lt;&lt;EODUMP;  
   footer goes here
EODUMP

sub PRINTRESULTS
{
print "&lt;table&gt;";
foreach $entr (@entries)
	{
	 		$phone = $entr-&gt;get('telephoneNumber');
  			$user = $entr-&gt;get('cn');
  			$email = $entr-&gt;get('mail');
  			$department = $entr-&gt;get('Department');
  			$address = $entr-&gt;get('address');
			$address = $address-&gt;[0];
			$department = $department-&gt;[0];
 			$phone = $phone-&gt;[0];
  			$user = $user-&gt;[0];
   			$email = $email-&gt;[0]; 
			$title = $entr-&gt;get('title');
			$title = $title-&gt;[0];
			$street = $entr-&gt;get('postalAddress');
			$street = $street-&gt;[0];
			
 			print "&lt;tr&gt;&lt;td&gt;&lt;a href=\"mailto:$email\"&gt;$user&lt;/a&gt;&lt;/td&gt;\n";
 			print "&lt;td&gt;$phone&lt;/td&gt;&lt;/tr&gt;\n";
			print "&lt;tr&gt;&lt;td&gt;$department, $title\n&lt;/td&gt;&lt;/tr&gt;\n";
			print "&lt;tr&gt;&lt;td&gt;$street&lt;/td&gt;\n";
			print "&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;\n";
		
	}
	print "&lt;/table&gt;";
}

sub LDAPsearch
 {
          my ($ldap,$searchString,$attrs) = @_ ;
          my $result = $ldap-&gt;search (
                base    =&gt; "$base",
                scope   =&gt; "sub",
                filter  =&gt; "$searchString",
                attrs   =&gt;  $attrs,
		sizelimit =&gt; $size_limit
                );
 }
&lt;/code&gt;</field>
<field name="codedescription">
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 &lt;a href=index.pl?node=chromatic&gt;Chromatic&lt;/a&gt; for giving me a fix for the Win32 problem with the entry's.</field>
<field name="codecategory">
networking code</field>
<field name="codeauthor">
Joseph Harnish
Big_Joe1008@yahoo.com</field>
</data>
</node>
