![]() |
Web Conference 2005Writing PHP for ITS/ASET Web servicesUsing The Penn State Directory (currently LDAP) |
![]() |
| <- Back - Using Penn State WebAccess | | | Up | | | WebAccess and LDAP - Next -> |
There is a central directory containing information on all Penn State students, faculty and staff. You may query it for information via http://www.psu.edu/directory/ and may change your own entry via https://www.work.psu.edu/. In addition to containing information useful to people, such as name, role and contact information, it contains data useful to programs such as the central Penn State email system.
The Penn State Directory currently uses the Lightweight Directory Access Protocol (LDAP). It used to be based on the Ph directory. More information on Penn State's use of LDAP is documented on http://aset.its.psu.edu/ldap/.
(Updated 2007/03/30) Note: The current server to use for LDAP enabled applications you design is dirapps.aset.psu.edu. This was a change from ldap.psu.edu which can still be used by directory enabled applications such as email clients (see also Configuring Clients). Please refer to http://aset.its.psu.edu/ldap/ for additional information.
You may query the directory from your PHP application and use
the information a variety of ways. You may use it to greet the user by
name, authorize users who have a particular entry or settings, such as
edupersonprimaryaffiliation=staff, etc.
The following code will query the Directory for people by their names, userids or email addresses:
<html>
<head>
<style>
<!--
p.border { border: solid 1px; }
// -->
</style>
</head>
<body>
<?php
if( isset( $_REQUEST['name'] ) or isset( $_REQUEST['userid'] ) or isset( $_REQUEST['email'] ) ){
if($_REQUEST['name'] !== "" and $_REQUEST['userid'] === "" and $_REQUEST['email'] === ""){
$query = "cn=*" . $_REQUEST['name'] . "*";
}elseif($_REQUEST['name'] === "" and $_REQUEST['userid'] !== "" and $_REQUEST['email'] === ""){
$query = "uid=" . $_REQUEST['userid'];
}elseif($_REQUEST['name'] === "" and $_REQUEST['userid'] === "" and $_REQUEST['email'] !== ""){
$query = "psmailid=" . $_REQUEST['email'] . "@psu.edu";
}else{
$query = "(&";
if($_REQUEST['name'] !== ""){
$query .= "(cn=*" . $_REQUEST['name'] . "*)";
}
if($_REQUEST['userid'] !== ""){
$query .= "(uid=" . $_REQUEST['userid'] . ")";
}
if($_REQUEST['email'] !== ""){
$query .= "(psmailid=" . $_REQUEST['email'] . "@psu.edu)";
}
$query .= ")";
}
$directory_connection = ldap_connect("dirapps.aset.psu.edu");
if($directory_connection){
// an anonymous bind for read/only access
$binding = ldap_bind($directory_connection);
// Base Distinguished Name (DN) for Penn State
$base_dn = "dc=psu,dc=edu";
$search_results = ldap_search($directory_connection, $base_dn, $query);
echo "Found " . ldap_count_entries($directory_connection, $search_results) . "<br>";
$results = ldap_get_entries($directory_connection,$search_results);
// only display the first 10
$max = $results['count'];
if( $max > 10 ){
$max = 10;
echo "Printing first 10<br>";
}
for ( $i=0; $i<$max; $i++ ){
print "<p class=border>";
if(isset( $results[$i]["cn"][0] )){
print "Name: " . $results[$i]["cn"][0] . "<br>";
}
if(isset( $results[$i]["uid"][0] )){
print "Userid: " . $results[$i]["uid"][0] . "<br>";
}
if(isset( $results[$i]["psmailid"]['count'] )){
for( $j=0; $j<$results[$i]["psmailid"]['count']; $j++ ){
print "Email: " . $results[$i]["psmailid"][$j] . "<br>";
}
}
print "</p>";
}
ldap_close($directory_connection);
}else{
echo "<p>Unable to connect to Penn State Directory server</p>";
}
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method=POST>
<p>Search for:</p>
<p>Name: <input name=name size=30 maxlength=40><br>
Userid: <input name=userid size=30 maxlength=40><br>
Email: <input name=email size=30 maxlength=40>@psu.edu<br>
<input type=submit value="Submit"></p>
</form>
</body>
</html>
Save the file as directory_search.php.
| <- Back - Using Penn State WebAccess | | | Up | | | WebAccess and LDAP - Next -> |
|
If you have any questions, feel free to ask me. Content by: Jeff D'Angelo <jcd@psu.edu> © 2005 Last update on: Fri Mar 30, 2007, 3:18:21 PM |
|