本文介绍了PHP认证具有LDAP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的PHP,甚至更多的LDAP和我需要做一个页面只能访问身份验证的用户。

我的一切工作是在控制台执行以下命令:

  $的ldapsearch -bDC = UT,DC = EE'-D'UID = USER,OU =人,DC = UT,DC = EE'-x -w密码''UID =用户说明

到目前为止,我已经成功地使用ldap_connect连接,并确定它使用的协议版本3。我曾与ldap_search和的ldap_bind发挥各地,但所有尝试均未成功。我开始认为这是因为-x和-w选项。或者,也许是因为LDAP仍然是真正的混乱给我。

任何提示将是深深地,深深地AP preciated!


解决方案

  $ =用户名'用户';
$密码='passwd文件;
$ account_suffix =@ example.com;
$主机='ldap.example.com';
$ CON = ldap_connect($主机名);
如果trigger_error(无法连接到主机$,E_USER_WARNING)(is_resource($ CON)!);
ldap_set_option($ CON,LDAP_OPT_PROTOCOL_VERSION,3);
ldap_set_option($ CON,LDAP_OPT_REFERRALS,0);如果(的ldap_bind($ CON,$用户名。$ account_suffix,$密码))
{
    // 登录
}
ldap_close($ CON);

要使用安全连接,你可以看看这里我的帖子:Problems用PHP 安全绑定到Active Directory - 我的code应该是在大多数系统中有效的

I'm relatively new to PHP and even more to LDAP and I need to make one page accessible only to authenticated users.

All I got working is the following command on console:

$ ldapsearch -b 'dc=ut,dc=ee' -D 'uid=USER,ou=People,dc=ut,dc=ee' -x -w 'PASSWORD' 'uid=USER' 'description'

So far I've successfully connected using ldap_connect and determined that it's using protocol version 3. I have played around with ldap_search and ldap_bind, but all the attempts are unsuccessful. I'm beginning to think it's because of the -x and -w options. Or maybe because LDAP is still really confusing to me.

Any hints will be deeply, deeply appreciated!

解决方案
$username = 'user';
$password = 'passwd';
$account_suffix = '@example.com';
$hostname = 'ldap.example.com';


$con =  ldap_connect($hostname);
if (!is_resource($con)) trigger_error("Unable to connect to $hostname",E_USER_WARNING);
ldap_set_option($con, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($con, LDAP_OPT_REFERRALS, 0);

if (ldap_bind($con,$username . $account_suffix, $password))
{
    // Logged in
}
ldap_close($con);

To utilize a secure connection, you can have a look at my post here: Problems with secure bind to Active Directory using PHP - my code should be valid on most systems.

这篇关于PHP认证具有LDAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:04