LDAP与PHP的连接问题

时间:2018-07-26 18:03:03

标签: php ldap ldap-query

我目前正在尝试连接到我们的Active Directory,以进行一些精美的搜索。我有所有用python编写的奇特搜索,现在我们正试图将其移植到PHP。我在连接ldap服务器时无法正常工作。我没有收到任何错误消息,也没有显示我的翔实回声,也没有我的页脚。任何帮助,将不胜感激!谢谢!

代码如下:

<?php
    include "src/header.php";
    echo "Well Hello-01<br>";

    if(isset($_POST['username'])){
        $User = 'AD\\' . $_POST['username'];
    }
    if(isset($_POST['password'])){
        $Pass = $_POST['password'];
    }



    echo "Username: " . $User . "<br>";
    echo "Password: " . $Pass . "<br>";

    $ldapconn = ldap_connect("ldap://ad.whatever.com")
         or die("Could not connect to LDAP server");

    if($ldapconn){  
        echo "Attempting Bind";
        //binding to ldap 
        $ldapbind = ldap_bind($ldapconn, $User, $Pass);

        //Verify Bind
        if($ldapbind){
            echo "LDAP bind successfull...";
        }else{
            echo "LDAP bind failed...";
        }
    }else{
        echo "Fail";
    }

    include "src/footer.php";
?>

</body>`

对于这里值得的是输出到屏幕上的内容:Screenshot

1 个答案:

答案 0 :(得分:0)

我前一段时间编写了这段代码,也许它可以帮助您:

<?php
    $username = $_POST['USERNAME'];
    $password = $_POST['USERPASS'];
    $server = 'AD_SERVER_IP_GOES_HERE';
    $domain = '@MY_DOMAIN.COM'; 
    $port = 389; //default connection port
    $dn = "DC=MY_DOMAIN,DC=COM";
    $filter = "(&(samaccountname=".$username."))";
    $params = array("sn","givenName","samAccountName",
                     "mail","displayName","department",
                     "title","company","streetAddress",
                     "department","memberOf");

    /*these are parameters you want to retrieve from a given user*/


    $connection = ldap_connect($server, $port);
        if (!$connection) {
        echo 'no_server';

    }


    $bind = @ldap_bind($connection, $username.$domain, $password);
    if (!$bind) {
        echo 'user_error';
    }
    else
    {
        $query_user = ldap_get_entries($connection,ldap_search($connection,$dn,$filter,$params));

        print_r($query_user);
    }

    // Close conection

    ldap_close($connection);

}

?>