PHP 5.3.5 ldap_search():搜索:无法联系LDAP服务器

时间:2011-10-11 02:27:28

标签: php ubuntu ldap

我有一个LDAP PHP类,适用于我们公司的所有其他PHP安装。但是,代码现在停止工作,我从SuSE更改为Ubuntu 11.04。 PHP版本是5.3.5。

我尝试连接到我们的LDAP服务器。连接到它后,我尝试运行ldap_search。它失败并显示错误消息'ldap_search():搜索:无法联系LDAP服务器'。 我使用wireshark查看了流量,可以看到php和ldap之间发生的通信,唉没有连接发生。

感谢您的想法, 乌韦

以下是我使用的代码:

class ldap{
    private $ldap_conn;
    private $ldaphost;
    private $ldapport;

    public function __construct ()
    {
      $this->ldaphost = 'ldaps://ourserver.co.nz';  // obviously a fake name 
      $this->ldapport = 636;
      $this->ldap_conn = ldap_connect($this->ldaphost, $this->ldapport) or die("Could not connect to $this->ldaphost");
    }

    public function signin ($username,$password, $applicationName)
    {
        $dn = "o=MY_COM";
        $filter="(cn=$username)";

        $justthese = array("dn","fullname", "mail");

        $sr=ldap_search($this->ldap_conn, $dn, $filter, $justthese);

        $people = ldap_get_entries($this->ldapconn, $sr);

        if (!$people || count($people) == 0 || $people['count'] == 0)
        {  //Nothing found, quit.
            return AUTH_ERROR_NO_DN;
        }

        $dn = ($people[0]['dn']);

        if (($dn!='')&&($password!=''))
        {
            $loginStatus = @ldap_bind($this->ldapconn, $dn, $password);
            if ($loginStatus)
            {
                $fullname = $people[0]['fullname'][0];
                if ($fullname=='')
                {
                    $fullname =$dn;
                }
                $user = array(
                  'logged_in_dn'  => $dn,
                  'password'      => $password,
                  'username'      => $fullname,
                  'cn'            => $username,
                  'mail'          => $people[0]['mail'][0],
                  'status'        => 'AUTHENTICATED'
                  );
            } else
            {
                $user = array(
                  'status'=> array('AUTH_ERROR_WRONG_PASSWORD'),
                  'details'       => array(             
                    'logged_in_dn'  => '',
                    'password'      => '',
                    'username'      => '',
                    'cn'            => '',
                    'mail'          => '',
                    'status'        => 'AUTHENTICATED'
                    )
                  );    
            }
        } else
        {
                $user = array(
                  'status'=> array('AUTH_ERROR_NO_DN'),
                  'details'       => array(             
                    'logged_in_dn'  => '',
                    'password'      => '',
                    'username'      => '',
                    'cn'            => '',
                    'mail'          => '',
                    'status'        => 'AUTHENTICATED'
                    )
                  );    
        }
        return $user;
    }

这是执行方法的单元测试: class unit_ldapConnectTest扩展了TadPhpUnitTestCase

{

  function testLdapconnection () {  
  $ldap = new LDAP();
  $user = $ldap->signin('myname','password','');


  }
}

1 个答案:

答案 0 :(得分:5)

这可能是LDAP版本问题。

致电ldap_connect后,请致电:

ldap_set_option($this->ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);