<?php
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connection

$server = '472lang1.lang.uc.edu';
$name = $_POST['username'];
$searchstring="(cn=$name)";
$attnames=array("givenname","surname","cn","dn","mail","passwordexpirationtime","logintime","logingraceremaining","logindisabled");

$ds=ldap_connect($server);  // must be a valid LDAP server!

if ($ds) { 
	//ldap_search searches sub.  
	//ldap_list searches one level.
	//ldap_read searches base.
    $r=ldap_search($ds,"ou=Users,o=ucl", $searchstring, $attnames);  
	if($r) echo "ldap_search success<br>";
	
    echo "Number of entires returned is ".ldap_count_entries($ds,$r)."<p>";
    echo "<INPUT TYPE=\"button\" VALUE=\"Search Again\" onClick=\"history.go(-1);\"> ";
    echo "<br><br>";
    $entries = ldap_get_entries($ds, $r);
    echo "Data for ".$entries["count"]." items returned:<p>";

    for ($i=0; $i<$entries["count"]; $i++) {
        echo "<hr><p>";
		// Any multi-valued object will return like the following:
		// $entries[0]['mail']['count']=2
		// $entries[0]['mail'][0]='a@b.com'
		// $entries[0]['mail'][1]='b@b.com'
		foreach($attnames as $attname){
			if(isset($entries[$i][$attname])){
				if(is_array($entries[$i][$attname])){
					for($j=0;$j<$entries[$i][$attname]["count"];$j++){
						echo $attname." entry is: ". $entries[$i][$attname][$j] ."<br>";
					}
				}else{
					echo $attname." entry is: ".$entries[$i][$attname]."<br>";
				}
			}
		}
		echo "</p>";
    }
    ldap_close($ds);

} else {
    echo "<h4>Unable to connect to LDAP server</h4>";
}
?>
