我有一个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','');
}
}
解决方法:
这可能是LDAP版本问题.
在调用ldap_connect之后调用:
ldap_set_option($this->ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);