代码之家  ›  专栏  ›  技术社区  ›  Hussain Akbar

使用PHP中的OpenLDAP进行身份验证

  •  0
  • Hussain Akbar  · 技术社区  · 8 年前

    使用CentOS 7.5、Apache 2.4.6。在虚拟机中运行。没有SSL。

    我跟着 https://linuxhostsupport.com/blog/how-to-install-ldap-on-centos-7/

    在PHP中,当我进行匿名绑定并发出ldap_搜索时,我看到了用户的条目。

    当与userid绑定时&密码,则功能失败。

    代码如下:

    $ds = ldap_connect("localhost");  // must be a valid LDAP server!
    echo "LDAP Server connection result is " . $ds . "<br />";
    
    if ($ds) { 
      echo "Binding ..."; 
      $r=ldap_bind($ds);     // this is an "anonymous" bind, typically
                           // read-only access
    echo "Bind result is " . $r . "<br />";
    
    echo "Searching for (sn=*) ...";
    // Search surname entry
    $sr=ldap_search($ds, "o=Sapphire, ou=karachi", "sn=*");  
    echo "Search result is " . $sr . "<br />";
    
    echo "Number of entries returned is " . ldap_count_entries($ds, $sr) . "<br />";
    
    echo "Getting entries ...<p>";
    $info = ldap_get_entries($ds, $sr);
    echo "Data for " . $info["count"] . " items returned:<p>";
    
    for ($i=0; $i<$info["count"]; $i++) {
        echo "dn is: " . $info[$i]["dn"] . "<br />";
        echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
        echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
    }
    
    echo "Binding as $userid ...  ";
    if( $r = ldap_bind($ds, "uid=hussain,ou=People,dc=karachi,dc=sapphire", $password) ){
      echo "Userid or Password is valid";
    }else{
      echo "Userid or Password is not valid";
    }
    
    echo "Closing connection";
    ldap_close($ds);
    
    } else {
      echo "<h4>Unable to connect to LDAP server</h4>";
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Hussain Akbar    8 年前

    找到了答案。我需要补充一点

    ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
    

    推荐文章