elliott.david 2020-01-21 09:40 采纳率: 25%
浏览 51

PHP | LDAP | 在AD中进行实时搜索

I am using the live search function for my MySQL database data. But to go directly to the source I do not want a file or db in between and I went directly to the Active Directory itself.

The searching is working but after you searched for something like username, it gives the correct output, the data resets after 2/3 seconds. So the input type text is still filled in but it's showing all the results.

Does anyone can help me with this or can optimize the code?

INDEX.PHP

<!DOCTYPE html>
<?php
    session_start();
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Live Search</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
    </head>
    <body>
        <div class="container">
            <br />
            <br />
            <br />
            <h2 align="center">Live Data Search Active Directory</h2><br />
            <div class="form-group">
                <div class="input-group">
                    <span class="input-group-addon">Search</span>
                    <input type="text" name="search_text" id="search_text" placeholder="Search by Customer Details" class="form-control" />
                </div>
            </div>
            <br />
            <div id="result"></div>
        </div>
        <div style="clear:both"></div>
        <br />

        <br />
        <br />
        <br />
    </body>
</html>
<script>
$(document).ready(function(){
    load_data();
    function load_data(query)
    {
        $.ajax({
            url:"fetch.php",
            method:"post",
            data:{query:query},
            success:function(data)
            {
                $('#result').html(data);
            }
        });
    }

    $('#search_text').keyup(function(){
        var search = $(this).val();
        if(search != '')
        {
            load_data(search);
        }
        else
        {
            load_data();            
        }
    });
});
</script>

My fetch.php file with all the links to AD. Ofcourse crendentials and server are filled in and binding is working.

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <div style="overflow-x:auto;">
    </head>
</html>
<?php

$output = "";

$ldap_password = "<username>";
$ldap_username = "<password>";
$ldap_connection = ldap_connect("<ldapserver>");

if (FALSE === $ldap_connection){
    echo "Unable to connect to the ldap server";
}

ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die("Unable to set LDAP protocol version");
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);

if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password))
{
    if (isset($_POST["query"]))
    {
        $search = $_POST["query"];
        $search_filter = "(&(objectCategory=person)(|(sAMAccountName=*".$search.")(sAMAccountName=".$search."*)(l=*".$search."*)))";
    }
    else
    {
        $search_filter = "(&(objectCategory=person)(|(sAMAccountName=*)))";
    }

    $output .= '<table id="customers"><tr><th>Username</th><th>Last Name</th><th>First Name</th><th>Company</th><th>Office</th><th>Department</th><th>Mobile</th><th>Telephone</th><th>E-Mail Address</th></tr>';

    $ldap_base_dn = "OU=NL,DC=global,DC=com";
    $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter);

    if (FALSE !== $result){
        $entries = ldap_get_entries($ldap_connection, $result);

    //var_dump($entries);

    //For each account returned by the search
    for ($x=0; $x<$entries["count"]; $x++){

        //Windows Username
        $LDAP_samaccountname = "";

        if (!empty($entries[$x]["samaccountname"][0])) {
        $LDAP_samaccountname = $entries[$x]["samaccountname"][0];
        if ($LDAP_samaccountname == "NULL"){
        $LDAP_samaccountname= "";
        }
        } else {
        //#There is no samaccountname s0 assume this is an AD contact record so generate a unique username

        $LDAP_uSNCreated = $entries[$x]["usncreated"][0];
        $LDAP_samaccountname= "CONTACT_" . $LDAP_uSNCreated;
        }

        //Last Name
        $LDAP_LastName = "";

        if (!empty($entries[$x]["sn"][0])) {
        $LDAP_LastName = $entries[$x]["sn"][0];
        if ($LDAP_LastName == "NULL"){
        $LDAP_LastName = "";
        }
        }

        //First Name
        $LDAP_FirstName = "";

        if (!empty($entries[$x]["givenname"][0])) {
        $LDAP_FirstName = $entries[$x]["givenname"][0];
        if ($LDAP_FirstName == "NULL"){
        $LDAP_FirstName = "";
        }
        }

        //Company
        $LDAP_CompanyName = "";

        if (!empty($entries[$x]["company"][0])) {
        $LDAP_CompanyName = $entries[$x]["company"][0];
        if ($LDAP_CompanyName == "NULL"){
        $LDAP_CompanyName = "";
        }
        }

        //Department
        $LDAP_Department = "";

        if (!empty($entries[$x]["department"][0])) {
        $LDAP_Department = $entries[$x]["department"][0];
        if ($LDAP_Department == "NULL"){
        $LDAP_Department = "";
        }
        }

        //Office
        $LDAP_Office = "";

        if (!empty($entries[$x]["l"][0])) {
        $LDAP_Office = $entries[$x]["l"][0];
        if ($LDAP_Office == "NULL"){
        $LDAP_Office = "";
        }
        }

        //Job Title
        $LDAP_JobTitle = "";

        if (!empty($entries[$x]["title"][0])) {
        $LDAP_JobTitle = $entries[$x]["title"][0];
        if ($LDAP_JobTitle == "NULL"){
        $LDAP_JobTitle = "";
        }
        }

        //Mobile Number
        $LDAP_CellPhone = "";

        if (!empty($entries[$x]["mobile"][0])) {
        $LDAP_CellPhone = $entries[$x]["mobile"][0];
        if ($LDAP_CellPhone == "NULL"){
        $LDAP_CellPhone = "";
        }
        }

        //Telephone Number
        $LDAP_DDI = "";

        if (!empty($entries[$x]["telephonenumber"][0])) {
        $LDAP_DDI = $entries[$x]["telephonenumber"][0];
        if ($LDAP_DDI == "NULL"){
        $LDAP_DDI = "";
        }
        }

        //Email address
        $LDAP_InternetAddress = "";

        if (!empty($entries[$x]["mail"][0])) {
        $LDAP_InternetAddress = $entries[$x]["mail"][0]; 
        if ($LDAP_InternetAddress == "NULL"){
        $LDAP_InternetAddress = "";
        }
        }

        $output .= '<tr><td><strong>' . $LDAP_samaccountname .'</strong></td><td>' .$LDAP_LastName.'</td><td>'.$LDAP_FirstName.'</td><td>'.$LDAP_CompanyName.'</td><td>'.$LDAP_Office.'</td><td>'.$LDAP_Department.'</td><td>'.$LDAP_CellPhone.'</td><td>'.$LDAP_DDI.'</td><td>'.$LDAP_InternetAddress.'</td></tr>';


        } //END for loop
        echo $output;
        } //END FALSE !== $result
        echo("</table>"); //close the table     
} 
?>

The loop is to display multiple results.

As you can see the result is good, but after 2/3 sec it resets and shows all data instead of the "sbx" value. (data is confidential so not shown, but it's corect)

Greets, Stef

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?