警告:ldap_bind():无法绑定到服务器:PHP 和 LDAP 凭据无效

发布于 2024-12-27 15:40:07 字数 1469 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最终幸福 2025-01-03 15:40:07

我知道这是一个相当老的问题,如果您仍然需要答案,那么如果您在单个 php 文件中运行此代码会发生什么?

$username = 'hello';
$password = '123123';
$server = '192.168.32.4';
$domain = '@yourdomain.local';
$port = 389;

$connection = ldap_connect($server, $port);
if (!$connection) {
    exit('Connection failed');
}

// Help talking to AD
ldap_set_option($connection , LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connection , LDAP_OPT_REFERRALS, 0);

$bind = @ldap_bind($connection, $username.$domain, $password);
if (!$bind) {
    exit('Binding failed');
}

// This is where you can do your work
echo 'Hello from LDAP';

ldap_close($connection );

更多信息请参见此处

I know it is a pretty old question and if you still need an answer then what happens if you run this code in a single php file?

$username = 'hello';
$password = '123123';
$server = '192.168.32.4';
$domain = '@yourdomain.local';
$port = 389;

$connection = ldap_connect($server, $port);
if (!$connection) {
    exit('Connection failed');
}

// Help talking to AD
ldap_set_option($connection , LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connection , LDAP_OPT_REFERRALS, 0);

$bind = @ldap_bind($connection, $username.$domain, $password);
if (!$bind) {
    exit('Binding failed');
}

// This is where you can do your work
echo 'Hello from LDAP';

ldap_close($connection );

More info is here.

山人契 2025-01-03 15:40:07

我使用了这些函数:

function authenticate($username, $password){
    include 'conf/config.inc.php';

    $ldap_Userdn = getUserDN($username);

    if($ldap_Userdn!=""){
        $ldap_con = ldap_connect($ldap_hostname,$ldap_port);
        ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);

        if(ldap_bind($ldap_con, $ldap_Userdn, $password)){
            return true; 
        } else  {
            //echo "<br>Error bind checkPassword function<br>";
            return false;
        }
    } else {
        echo "Error to find user DN" . ldap_error($ldap_con);
    }

    ldap_close($ldap_con);
}

function getUserDN($username){
    include 'conf/config.inc.php';

    $data = "";

    $ldap_con = ldap_connect($ldap_hostname,$ldap_port);
    ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldap_con, LDAP_OPT_REFERRALS, 0);

    if(ldap_bind($ldap_con, $ldap_dn, $ldap_password)){
        $filter="(cn=$username)";
        $dn=$ldap_search; //even if it seems obvious I note here that the dn is just an example, you'll have to provide an OU and DC of your own

        $res = ldap_search($ldap_con, $ldap_search, $filter);
        $first = ldap_first_entry($ldap_con, $res);
        $data = ldap_get_dn($ldap_con, $first);

    } else {
        echo "<br>Error bind getUserDN function<br>" . ldap_error($ldap_con);
    }

    ldap_close($ldap_con);
    return $data;
}

这是我的 config.inc.php

<?php
$ldap_hostname = "my openldap IP";
$ldap_port = "389";
$ldap_dn = "cn=Manager,dc=mydomain,dc=com";
$ldap_search = "dc=mydomain,dc=com";
$ldap_password ="my password";
?>

I used these functions:

function authenticate($username, $password){
    include 'conf/config.inc.php';

    $ldap_Userdn = getUserDN($username);

    if($ldap_Userdn!=""){
        $ldap_con = ldap_connect($ldap_hostname,$ldap_port);
        ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);

        if(ldap_bind($ldap_con, $ldap_Userdn, $password)){
            return true; 
        } else  {
            //echo "<br>Error bind checkPassword function<br>";
            return false;
        }
    } else {
        echo "Error to find user DN" . ldap_error($ldap_con);
    }

    ldap_close($ldap_con);
}

function getUserDN($username){
    include 'conf/config.inc.php';

    $data = "";

    $ldap_con = ldap_connect($ldap_hostname,$ldap_port);
    ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldap_con, LDAP_OPT_REFERRALS, 0);

    if(ldap_bind($ldap_con, $ldap_dn, $ldap_password)){
        $filter="(cn=$username)";
        $dn=$ldap_search; //even if it seems obvious I note here that the dn is just an example, you'll have to provide an OU and DC of your own

        $res = ldap_search($ldap_con, $ldap_search, $filter);
        $first = ldap_first_entry($ldap_con, $res);
        $data = ldap_get_dn($ldap_con, $first);

    } else {
        echo "<br>Error bind getUserDN function<br>" . ldap_error($ldap_con);
    }

    ldap_close($ldap_con);
    return $data;
}

an this is my config.inc.php:

<?php
$ldap_hostname = "my openldap IP";
$ldap_port = "389";
$ldap_dn = "cn=Manager,dc=mydomain,dc=com";
$ldap_search = "dc=mydomain,dc=com";
$ldap_password ="my password";
?>
若相惜即相离 2025-01-03 15:40:07

检查您的登录和密码是否正确。
并在登录之前添加域。请参阅示例底部(总部\登录):

<?php
$login = 'HQ\student';
$password = 'MYPASS';
        
$ldap_link = ldap_connect('pdc.bc') or die("Sorry, could not connect to LDAP server.");

$ldapbind = @ldap_bind($ldap_link, $login, $password) or die ("Error trying to bind: ".ldap_error($ldap_link));
?>

Check whether your login and pass correct.
And before the login add domain. See in example bottom (HQ\login):

<?php
$login = 'HQ\student';
$password = 'MYPASS';
        
$ldap_link = ldap_connect('pdc.bc') or die("Sorry, could not connect to LDAP server.");

$ldapbind = @ldap_bind($ldap_link, $login, $password) or die ("Error trying to bind: ".ldap_error($ldap_link));
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文