dousilie9522 2014-05-18 17:48
浏览 51

使用LDAP和PHP查询Active Directory信息

I have a question in regards to LDAP implementation using PHP. I just recently started reading up on LDAP and am still having trouble understanding how it works. I am developing a new application that will grant access to users only if they are found in Active directory. The code I have posted below determines if the username and password the user enters is in Active Directory. This code works, however now I would like to pull the first name of the user based on what credentials were entered. The reason I would like to pull this information is so that when the user logs in they are redirected to another page (ie. dashboard.php) that will display content and the phrase "Welcome "First Name of User". I am just not sure how I would have to go about pulling this information. If anyone could provided feedback on how I should go about doing this it would be greatly appreciated.

Connecting to LDAP sever:

<?php
$ldaphost = "name of host"; //LDAP Host
$ldapport = 389; //LDAP Port Number                

$ldapconn = ldap_connect($ldaphost, $ldapport); //Connect to LDAP Host on LDAP Port Number

// Check LDAP connection
if(!$ldapconn) {
    die("Unsuccessful connection to " . $ldaphost . " on port " . $ldapport . "<br />");    
} 
?>

Validate if user is in Active Directory:

<?php
include 'ldap_connect.php';

$username = $_POST['username'];
$password = $_POST['password'];

$ldapbind = ldap_bind($ldapconn, $username, $password);

if ($ldapbind) {
    header("Location: ../dashboard.php");
} else {
    print "Access Denied!";
}
?>
  • 写回答

1条回答 默认 最新

  • dongra1984 2014-05-19 06:42
    关注

    You will have to do an ldap_search for the user to get the attributes of the user.

    Something like this:

    $ldapresults = ldap_search($ldapconn, $baseDn, 'samAccountName=' . $username, array('sn'), 0, 0, 10);
    if (! $ldapresults) {
        die('No results found');
    }
    $results = ldap_get_entries($ldapconn, $ldapresults);
    var_dump($results);
    

    You should then see the content of $result.

    Perhaps you will have to play around with $baseDn, the filter (samAccountName=$username) and the attributes retrieved (array('sn')) to get the values you are looking for right.

    I've created a gist that does ldap-authentication and retrieval of user-information in one go. Have a look at https://gist.github.com/heiglandreas/5689592

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据