Listing D
<?php
// LDAP variables
$ldap[‘user’]              = ‘uname’;
$ldap[‘pass’]              = ‘password’;
$ldap[‘host’]              = ‘ldap.example.com’;
$ldap[‘port’]              = 389;
$ldap[‘dn’]          = ‘cn’.$ldap[‘user’].’,ou=Department,o=Company Name’;
$ldap[‘base’]              = ‘’;
 
// connecting to ldap
$ldap[‘conn’] = ldap_connect( $ldap[‘host’], $ldap[‘port’] )
or die( “Could not connect to server {$ldap[‘host’]} );
 
// binding to ldap
$ldap[‘bind’] = ldap_bind( $ldap[‘conn’], $ldap[‘dn’], $ldap[‘pass’] );
 
if( !$ldap[‘bind’] )
{
    echo ldap_error( $ldap[‘conn’] );
    exit;
}
 
// search for the user on the ldap server and return all
// the user information
$ldap[‘result’] = ldap_search( $ldap[‘conn’], $ldap[‘base’], ‘uid=’.$ldap[‘user’] );
 
 
 
 
if( $ldap[‘result’] )
{
    // retrieve all the entries from the search result
    $ldap[‘info’] = ldap_get_entries( $ldap[‘conn’], $ldap[‘result’] );
}
else
{
    echo ldap_error( $ldap[‘conn’] );
    exit;
 
}
 
if( $ldap[‘info’] )
{
    // Add the user’s department name and email address
    // to the session
    $_SESSION[‘userdept’] = $ldap[‘info’][0][‘department’][0];
    $_SESSION[‘usermail’] = $ldap[‘info’][0][‘mail’][0];
}
else
{
    echo ldap_error( $ldap[‘conn’] );
    exit;
}
 
// close connection to ldap server
$ldap_close( $ldap[‘conn’] );
 
?>