最近离职了,但是踩了坨狗屎,接了一个迈瑞的微信项目,赚了点钱。其中他们要求通过ldap对接自己的用户信息,微信项目这边没有用户的概念。
开发用的是php,但php默认没有开启ldap支持,需要在php.ini中开启ldap支持。
extension=php_ldap.dll //去掉前面的分号
然后重启服务器,报了一个错,没有找到Libsasl.Dll。
将php中libsasl.dll拷到system32中,再次重启,phpinfo可以看到ldap扩展了。
ldap验证代码如下:
<?php
class adLDAP {
protected $_account_suffix = "";
protected $_base_dn = "dc=mindray,dc=com";
protected $host = array ("127.0.0.1");
protected $port = '389';
protected $_conn;
function __construct($options=array()){
if (count($options)>0){
if (array_key_exists("base_dn",$options)){ $this->_base_dn=$options["base_dn"]; }
if (array_key_exists("host",$options)){ $this->host=$options["host"]; }
if (array_key_exists("port",$options)){ $this->port=$options["port"]; }
}
if ($this->ldap_supported() === false) {
echo 'ldap not supported';
return false;
}
return $this->connect();
}
function __destruct(){
$this->close();
}
public function connect() {
$host = $this->random_host();
$port = $this->port;
$this->_conn = ldap_connect($host,$port);
return (true);
}
public function close() {
ldap_close ($this->_conn);
}
/**
* Validate a user's login credentials
*
* @param string $username A user's AD username
* @param string $password A user's AD password
* @param bool optional $prevent_rebind
* @return bool
*/
public function authenticate($username, $password) {
if (empty($username) || empty($password)) return false;
$r = @ldap_search( $this->_conn, $this->_base_dn, 'uid=' . $username);
if ($r) {
$result = @ldap_get_entries( $this->_conn, $r);
if ($result[0]) {
if (@ldap_bind( $this->_conn, $result[0]['dn'], $password) ) {
//return $result[0];
return true;
}
}
}
}
protected function random_host(){
mt_srand(doubleval(microtime()) * 100000000); // For older PHP versions
return ($this->host[array_rand($this->host)]);
}
protected function ldap_supported() {
if (!function_exists('ldap_connect')) {
return (false);
}
return (true);
}
}
ps:为有需要的人士分享!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/98463.html