[英]LDAP Authentication using PHP
我正在嘗試使用PHP使用LDAP身份驗證。
下面是我的代碼:
<?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!";}
}
?>
但這會引發以下錯誤:
PHP警告:
ldap_bind()
:無法綁定到服務器:無法聯系LDAP服務器
有什么想法可以解決嗎?
注意:正如我在某個論壇上遇到這個詞時,我們是否需要ldap.config
文件。 我的機器上沒有任何此類文件。 我在ext文件夾中有php_ldap.dll
並使用Windows
。
我猜ldap_connect()不需要協議,因此這個天真的補丁應該可以解決您的問題:
--- 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)
檢查官方文檔中的基本示例 。
我們在本地網絡中證明了這一點,並且效果很好。 例如,如果您將Ldap與Zentyal一起使用,請轉至https://serveriporname/Users/Composite/Settings
,然后選擇“用戶DN”中提供的選項,以便采用該地址,我們將其稱為$userdns
,您可能會證明以下代碼
<?php
//The variables are implicit
$ad = ldap_connect($ldap_server) ; //Ex: 10.0.0.1
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3) ;
//Using the provided user and password to login into LDAP server.
//For the dc, normally will be the domain.
$sr=ldap_search($ad, $userdns, "cn=*usuario*");
$info = ldap_get_entries($ad, $sr);
for ($i=0; $i<$info["count"]; $i++) {
/*Print out the user information here. If you rather to request by other field than cn take its name from here*/
print_r($info[$i]);
echo "<p><hr/></p>";
}
$ds = ldap_bind($ad,"uid=$ldap_user,$userdns",$ldap_pass);
if($ds){
echo "<h4>$ldap_user connect to LDAP server \"$ldap_domain\"</h4>";
} else {
echo "<h4>Unable to connect to LDAP server</h4>";
}
ldap_close($ad);
?>
正如Minras所說,您綁定的憑證錯誤。 嘗試這樣的事情:
$ldaprdn = 'cn=binder,dc=domain,dc=com'; // ldap rdn or dn or proxy agent or admin
$ldappass = 'password'; // associated password
// connect to ldap server
$ldapconn = ldap_connect("54.85.xx.xx")
or die("Could not connect to LDAP server.");
// Set some ldap options for talking to
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
if ($ldapconn) {
// binding to ldap server
$ldapbind = @ldap_bind($ldapconn, $ldaprdn, $ldappass);
// verify binding
if ($ldapbind) {
echo "Done..\n</h1>";
} else {
echo "Damn you LDAP...\n";
}
}
我不確定您是否仍然需要答案,但是我想補充一下我的經驗。
$username = 'bentcoder';
$password = '123123';
$server = '192.168.32.4';
$domain = '@yourdomain.com';
$port = 389;
$ldap_connection = ldap_connect($server, $port);
if (! $ldap_connection)
{
echo '<p>LDAP SERVER CONNECTION FAILED</p>';
exit;
}
// Help talking to AD
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);
$ldap_bind = @ldap_bind($ldap_connection, $username.$domain, $password);
if (! $ldap_bind)
{
echo '<p>LDAP BINDING FAILED</p>';
exit;
}
// You can work now!!!
ldap_close($ldap_connection);
筆記:
php_ldap
擴展名應在php.ini中啟用。 [extension = php_ldap.dll] 參考文獻:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.