duanpuchun5275 2012-09-04 07:48
浏览 252

使用PHP进行LDAP身份验证

I am trying to use LDAP authentication using PHP.
Below is my code:

<?php

$ldaphost = 'ldap://ldapServer';
$ldapport = 389;

$ds = ldap_connect($ldaphost, $ldapport)
or die("Could not connect to $ldaphost");
    ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
//ldap_set_option($ds, LDAP_OPT_DEBUG_LEVEL, 7);
if ($ds) 
{
    $username = "testuser@domain.com";
    $upasswd = "testpass";

    $ldapbind = ldap_bind($ds, $username, $upasswd);


    if ($ldapbind) 
        {print "Congratulations! $username is authenticated.";}
    else 
        {print "Access Denied!";}


}
?>

But it raises the below error:

PHP Warning: ldap_bind(): Unable to bind to server: Can't contact LDAP server

Any idea as how can I get it resolved?

Note: Do we need ldap.config file somewhere as I came across this term on some forum. I don't see any such file on my machine. I have php_ldap.dll in ext folder and using Windows.

  • 写回答

5条回答 默认 最新

  • doumei7420 2012-09-04 08:54
    关注

    I guess ldap_connect() doesn't requires the protocol, so this naive patch should solve your issue:

    --- ldap.php.bak    2012-09-04 10:52:29.563203493 +0200
    +++ ldap.php    2012-09-04 10:52:46.807203766 +0200
    @@ -1,6 +1,6 @@
     <?php
    
    -$ldaphost = 'ldap://ldapServer';
    +$ldaphost = 'ldapServer';
     $ldapport = 389;
    
     $ds = ldap_connect($ldaphost, $ldapport)
    

    Check the basic example at the official documentation.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建