2012-06-01 38 views
1

我試圖通過與Active Directory LDAP驗證用戶身份的PHP Web應用程序。PHP_AUTH_USER和LDAP

<?php 
    $ldapconfig['host'] = 'ldapserv.xx.uni.edu'; 
    $ldapconfig['port'] = 389; 
    $ldapconfig['basedn'] = 'dc=xx, dc=uni, dc=edu'; 
    $ldapconfig['authrealm'] = 'Secure Area'; 

    function ldap_authenticate() { 
     global $ldapconfig; 
     global $PHP_AUTH_USER; 
     global $PHP_AUTH_PW; 

     if ($PHP_AUTH_USER != "" && $PHP_AUTH_PW != "") {  
      [email protected]_connect($ldapconfig['host'],$ldapconfig['port']) or exit ("Error connecting to LDAP server."); 

      //Settings for AD 
      ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); 
      ldap_set_option($ds, LDAP_OPT_REFERRALS, 0); 

      $r = @ldap_search($ds, $ldapconfig['basedn'], 'uid=' . $PHP_AUTH_USER); 
      if ($r) { 
       $result = @ldap_get_entries($ds, $r); 
       if ($result[0]) { 
        if (@ldap_bind($ds, 'uni\\' . $PHP_AUTH_USER, $PHP_AUTH_PW)) { 

         return $result[0]; 
        } 
       } 
      } 
     } 
     header('WWW-Authenticate: Basic realm="'.$ldapconfig['authrealm'].'"'); 
     header('HTTP/1.0 401 Unauthorized'); 
     return NULL; 
    } 

    if (($result = ldap_authenticate()) == NULL) { 
     echo('Authorization Failed <br />'); 
     exit(0); 
    } 
    echo('Authorization success'); 
    print_r($result); 

?> 

此代碼只是提示用戶輸入用戶名/密碼,除非他們單擊取消。我究竟做錯了什麼?

回答

0

我能得到與session_register(和更簡單的代碼)的工作日誌中的身份驗證

<?php 

if (isset($_POST['submitted'])) { 

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

    $ldap = ldap_connect("ldapserv.xx.uni.edu", 389) or exit ("Error connecting to LDAP server."); 

    //Settings for AD 
    ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); 
    ldap_set_option($ds, LDAP_OPT_REFERRALS, 0); 


    if($bind = ldap_bind($ldap, 'uni\\'.$username, $password)) { 
     //Log them in! 
     session_register("username"); 
     session_register("password"); 
     header("Location: https://" . $_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'], 0, -9) . "index.php"); 
     exit; 

    } else { 

     echo('Invalid username or password.<br /><br />'); 
    } 
} 
?> 
1

最近的PHP版本不再供應$ PHP_AUTH_USER和$ PHP_AUTH_PW變量默認情況下,讓你的腳本甚至從來沒有獲取到LDAP檢查。卸下最後兩個「全局」行,並用$ _ SERVER [「PHP_AUTH_USER」]和$ _ SERVER [「PHP_AUTH_PW」]到處替換這些變量。

如果沒有幫助,刪除@字符,看是否有錯誤。