PHP / LDAP未获得任何结果且未显示错误

时间:2016-10-22 14:20:05

标签: php ldap

代码在其他环境中工作,但在另一个Intranet中尝试失败。

 <?php
    /**
     * Get a list of users from Active Directory.
     */
    $ldaprdn = $_POST['username'];
    $ldappass = $_POST['password'];
    $host_name =$_POST['host'];
    $ldap_connection = ldap_connect($host_name);
    if (FALSE === $ldap_connection){
        echo "Wrong";
    }

    // We have to set this option for the version of Active Directory we are using.
    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); // We need this for doing an LDAP search.
    require_once("connection.php");
    if(isset($_POST['submit']))
    {
    if (TRUE === ldap_bind($ldap_connection, $ldaprdn, $ldappass)){

        $ldap_base_dn = $_POST['basedn'];
        $search_filter = '(&(objectCategory=person)(samaccountname=*))';
    // $search_filter = '(&(objectCategory=person)(ObjectClass=Users)(samaccountname=*))'; tried using this also  no result
        $attributes = array();
        $attributes[] = 'givenname';
        $attributes[] = 'samaccountname';
        $attributes[] = 'lastlogon';
        $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
        if (FALSE !== $result){
            $entries = ldap_get_entries($ldap_connection, $result);
            $count = $entries["count"];
            for($i=0;$i<$entries["count"];$i++){
             print("<pre>");
             $name= $ldap_base_dn;
             //$name = $entries[$i]["givenname"][0];
             $samname = $entries[$i]["samaccountname"][0];
             print("Given Name: " .$entries[$i]["givenname"][0]." <br />");
             print("SAM Account Name: ".$entries[$i]["samaccountname"][0]." <br />");
             $fileTime = $entries[$i]["lastlogon"][0];
        $winSecs       = (int)($fileTime / 10000000); // divide by 10 000 000 to get seconds
        $unixTimestamp = ($winSecs - 11644473600); // 1.1.1600 -> 1.1.1970 difference in seconds
        //echo date(DateTime::RFC822, $unixTimestamp);
        $lasttime = date(DateTime::RFC822, $unixTimestamp);
             print("Last Logon Time: ".date(DateTime::RFC822, $unixTimestamp)." <br />");
              print("Computer Name: ".$host_name." <br />");
              $dt = new DateTime();
              $currenttime = $dt->format('Y-m-d H:i:s');;
            #print_r($entries);
            print "\n";
             print("</pre>");
             //
            $sql = "INSERT INTO report(currenttime,name,samname,lasttime,host_name)
             VALUES 
                  (?,?,?,?,?)
          ";
    //$sql = SELECT * FROM login;
            if (!$stmt = $con->prepare($sql)) 
            {
            echo 'Database prepared error';
            exit;
            }
            //$stmt = $con->prepare($sql);
        $stmt->bind_param('sssss',$currenttime_n,$name_n,$samname_n,$lasttime_n,$host_name_n);
    $currenttime_n = $currenttime;
    $name_n = $name;
    $samname_n = $samname;
    $lasttime_n = $lasttime;
    $host_name_n = $host_name;

            if (!$stmt->execute()) 
            {
            echo 'Database execute error';
            exit;
            }
        //header("Location:report.php");
        $stmt->close();

             //
             }

        }
        ldap_unbind($ldap_connection); // Clean up after ourselves.
    }
    }

    $message .= "Retrieved ". $count ." Active Directory users\n";
    echo $message . "<br>";
    ?>

0 个答案:

没有答案