Location: PHPKode > projects > Active Directory Phonebook > active-directory-phonebook-56296/ad_phone.php
<html>

<head>
<script>

function setFocus() {document.searchform.criteria.focus();}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<link rel="stylesheet" type="text/css" href="default.css">
<title>Your Company Phonebook</title></head>

<body onLoad="setFocus()" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0"> 

<table border="0" width="100%" id="table1" cellspacing="0" cellpadding="0">
	<tr>
		<td align="center" valign="top">
		<table border="0" width="760" id="table2" cellspacing="0" cellpadding="0">

			<tr>
				<td align="center">

<!-- this is the form that the user will input their search criteria-->
<form method="GET" action="<?php echo $PHP_SELF; ?>" name="searchform">
	<p>Search by: <input type="radio" value="l_name" name="params">Last Name*&nbsp;&nbsp;
	<input type="radio" value="f_name" name="params"> First Name*&nbsp;&nbsp;
	<input type="radio" value="district" name="params"> District*&nbsp;&nbsp;
	<input type="radio" value="exten" name="params"> Phone Extension*&nbsp;&nbsp;
	<input type="radio" value="all" name="params" checked> All of the Above*<br>
	<br>
&nbsp;<input type="text" name="criteria" size="30"><input type="submit" value="Submit" name="B1"><br>
	<br>
	* Your search does not need to be a complete word or number, the first few 
	letters are acceptable.</p>
</form>
				
				</td>
			</tr>
			

<?php

require ('ldap.config.php');

//if the search parameters are sent, then start the results output
if ($params)
{

	//details what to do given the different type of searches, firstname, last, etc.
	if ($params == "all")
	{
		$filter = "(|(sn=$criteria*)(givenname=$criteria*)(physicaldeliveryofficename=$criteria*)(telephonenumber=$criteria*))";
		if (!$sort_by){$sort_by = "sn";}
	}
	
	elseif ($params == "f_name")
	{
		$filter = "(givenname=$criteria*)";
		if (!$sort_by){$sort_by = "cn";}
	}
	
	elseif ($params == "l_name")
	{
		$filter = "(sn=$criteria*)";
		if (!$sort_by){$sort_by = "cn";}
	}

	elseif ($params == "exten")
	{
		$filter = "(telephonenumber=$criteria*)";
		if (!$sort_by){$sort_by = "telephonenumber";}
	}
		
	elseif ($params == "district")
	{
		$filter = "(physicaldeliveryofficename=*$criteria*)";
		if (!$sort_by){$sort_by = "physicaldeliveryofficename";}
	}

	//the fields that the search will pull from in ad
	$get_this=array("cn", "telephonenumber", "physicaldeliveryofficename", "description", "mail", "givenname", "sn");
	
	//make the ldap connection
	$connect = ldap_connect( $ldap_host, $ldap_port)
	         or exit(">>Could not connect to LDAP server<<");
	
	//for win2003
	ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
	
	//for win2003
	ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
	
	//this is where the username and password are used to make the ldap connection
	$bind = ldap_bind($connect, $ldap_user, $ldap_pass)
	     or exit(">>Could not bind to $ldap_host<<");
	
	//search ad ldap
	$read = ldap_search($connect, $base_dn, $filter, $get_this)
	     or exit(">>Unable to search ldap server<<");
	
	//sort the results by the user specifications
	ldap_sort($connect, $read, $sort_by);
	
	//get the entries and put into a multi dimensional array
	$info = ldap_get_entries($connect, $read);
		
	echo "<tr>";
	echo "<td align=\"center\">";
	echo "<p align=\"left\">";

	//print the number, if any, of results
	if ($info["count"] == 1)
	{
		echo $info["count"]." entry returned for \"$criteria\".<br><br>";
	}else{
		echo $info["count"]." entries returned for \"$criteria\".<br><br>";
	}

?>


</td>
			</tr>
			<tr>
				<td align="center">


<table border="1" width="100%" id="table4" bordercolorlight="#FFFFFF">
	<tr>
		<td width="13%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=sn"; ?>"><b>Last Name <? if ($sort_by == "sn" || $sort_by == "cn"){echo "»"; } ?></b></a></td>
		<td width="13%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=givenname"; ?>"><b>First Name <? if ($sort_by == "givenname"){echo "»"; } ?></b></a></td>
		<td width="30%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=physicaldeliveryofficename"; ?>"><b>Office <? if ($sort_by == "physicaldeliveryofficename"){echo "»"; } ?></b></a></td>
		<td width="22%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=description"; ?>"><b>Position <? if ($sort_by == "description"){echo "»"; } ?></b></a></td>
		<td width="22%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=telephonenumber"; ?>"><b>Phone <? if ($sort_by == "telephonenumber"){echo "»"; } ?></b></a></td>
	</tr>
	
<?php
//start the loop for printing the results
for ($a=0; $a<$info["count"]; $a++)
{
		//make every other row colored, feel free to change the colors
		if ($a % 2 == 0)
		{
			$color = "#FFFFCC";
		}else{
			$color = "#FFFFFF";
		}
		$email = $info[$a]["mail"][0];
		echo "<tr>";
		echo "<td bgcolor=\"$color\" width=\"13%\"><a href=\"mailto:$email\" title=\"Email ".$info[$a]["givenname"][0]." ".$info[$a]["sn"][0]."\"><b>".$info[$a]["sn"][0]."</b></a>&nbsp;</td>";
		echo "<td bgcolor=\"$color\" width=\"13%\"><a href=\"mailto:$email\" title=\"Email ".$info[$a]["givenname"][0]." ".$info[$a]["sn"][0]."\"><b>".$info[$a]["givenname"][0]."</b></a>&nbsp;</td>";
		echo "<td bgcolor=\"$color\" width=\"30%\">".$info[$a]["physicaldeliveryofficename"][0]."&nbsp;</td>";
		echo "<td bgcolor=\"$color\" width=\"22%\">".$info[$a]["description"][0]."&nbsp;</td>";
		echo "<td bgcolor=\"$color\" width=\"22%\">".$info[$a]["telephonenumber"][0]."&nbsp;</td>";
		echo "</tr>";
}
//diconnect from the ldap dbase
ldap_close($connect);
}

echo "</table>";
?>				
				</td>
			</tr>
		</table>
		</td>
	</tr>
</table>

</body>

</html>
Return current item: Active Directory Phonebook