跟小D每日学口语

PHP ADLogin

<?php

    $user = 'aaaa';
    $password = 'xxxx';

    $domain = 'b.a.com'; //设定域名
    $port = 3268;
    $basedn = 'dc=b,dc=a,dc=com'; //如果域名为“b.a.com”,则此处为“dc=b,dc=a,dc=com”
    $filter = "(SAMAccountName=aaaa)";
    $attr = array("thumbnailphoto");

    $conn = ldap_connect ( $domain,$port ) or die ( 'Could not connect to LDAP server.' );
   
    $bind = @ldap_bind ( $conn, "{$user}@{$domain}", $password ) or die ( 'Authorization failed! Please check your username or password!' );       
      
    if( $bind )
    {    
        $result = @ldap_search( $conn, $basedn, $filter, $attr);
        // retrieve all the entries from the search result
        $info = ldap_get_entries( $conn, $result );
             
        $entry = ldap_first_entry($conn,$result);       
    }
    else
    {
        echo 'error<br>';
        echo ldap_error( $conn );
        exit;
    }
    #print_r($info);
    
    if( $info)
    {
        $values = @ldap_get_values_len($conn, $entry, "thumbnailPhoto");
        
        echo '<img src="'.data_uri($values[0], 'image/png').'">';
        
        if(!$values) {           
            
            echo "no thumbnail photo<br>";
        }
 
        // Add the user’s department name and email address
        // to the session
        echo $info[0]['thumbnailPhoto'][0];
        
        foreach($info as $k => $v)  
        {    
              echo $v['cn'][0]."<br>" ;
              echo $v['displayname'][0]."<br>" ;
              echo $v['mail'][0]."<br>" ;  
              echo $v['department'][0]."<br>" ;
              echo $v['company'][0]."<br>" ;
              echo $v['streetaddress'][0]."<br>" ;
              echo $v['countryCode'][0]."<br>" ;
              echo $v['userPrincipalName'][0]."<br>" ;
              echo $v['manager'][0]."<br>" ;
              echo $v['sn'][0]."<br>" ;
              echo $v['pwdLastSet'][0]."<br>" ;
              echo $v['postalCode'][0]."<br>" ;
              echo $v['title'][0]."<br>" ;
              echo $v['thumbnailPhoto'][0]."<br>" ;
              echo $v['location'][0]."<br>" ;
       } 
         
    }
    else
    {
        echo ldap_error($conn);
        exit;
    }

    // close connection to ldap server
    $ldap_close( $conn );
    
    function data_uri($file, $mime) {  
        $base64   = base64_encode($file); 
        return ('data:' . $mime . ';base64,' . $base64);
    }
?>

 

posted @ 2014-08-25 18:14  Danny Chen  阅读(424)  评论(0编辑  收藏  举报