PHP ldap连接总是为真


PHP ldap connection always turns out true

我正在尝试与活动目录建立连接以验证用户身份。不管你输入什么,它都会帮你通过。

我甚至不确定我是否做了正确的连接。我目前使用[ip地址].[域名].nl作为主机名我不知道这是否可能,但是当我检查ldap_connect()错误时,它没有出现错误。

所以如果你们愿意看一下代码并给我一些提示,那就太好了。出于安全考虑,我省略了连接细节。

对不起,如果这是一个愚蠢的问题,但我不知道什么是错的。提前谢谢。
class LOGIN{
        public function login($data){
            $user = $data["username"];
            $password = $data["password"];
            $host = 'ldap://[ip adress]/[domain].nl';
            $domain = '[domain].nl';
            $basedn = 'dc=[domain],dc=nl';
            $ad = ldap_connect($host.$domain,389) or die('Could not connect to LDAP server.');
            ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
            ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);
            //$bind = ldap_bind($ad, "$user@$domain", $password) or die('Could not bind to AD.');
            if(@ldap_bind($ad, "$user"."@"."$domain", $password)){
                $result = "Authenticated";
            }else{
                $result = "Invalid Credential";
            }               
            return $result;
        }
    }
}
$login = new LOGIN; 

这是登录的表单:

<?php 
if(isset($_POST['username']) && isset($_POST['password']) && !empty($_POST['username']) && !empty($_POST['password'])){
require_once("classes/class_login.php");
$result = $login->login($_POST);
}   
?>
<form action="#" method="POST">
    <?php if(!empty($result)){echo "<p>".$result."</p>";} ?>
    <pre>
    <label for="username">Username: </label><input id="username" type="text" name="username" /> 
    <label for="password">Password: </label><input id="password" type="password" name="password" />       
    <input type="submit" name="submit" value="Submit" />
    </pre>
</form>

你是对的,api可能有问题,它总是返回true。这就是我们使用的工作实现。

    define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032);
    $conn = "ldap://whatever.com";   // 
    $port = "389";        // by default port 389
    $version = "3";       // by default 3
    $referral = "0"; // by default 0
    $user = "username";
    $password = "password";
    if ($user && $password) {
        //Connect LDAP Server
        echo " connecting to ldap mdec<br/> ";
        $connect = ldap_connect($conn, $port);
        ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, $version);
        ldap_set_option($connect, LDAP_OPT_REFERRALS, $referral);
        $bind = ldap_bind($connect, $user, $password);
        if ($bind) {
            echo "OK. ";
        } else {
            echo "couldn't bind.";
            if (ldap_get_option($connect, LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error)) {
                echo "Error connecting to LDAP: $extended_error";
            } else {
                echo "Error connecting to LDAP: No additional information is available.";
            }
        }
        ldap_close($connect);
    } else {
        echo "in else, do whatever you want to show to the user";
   }

ldap_connect接受提供的URI并检查其语法正确性。NO与服务器建立连接!

实际连接将在第一次调用服务器(通常是ldap_bind)时创建,因此连接问题可能会在ldap_bind命令中出现!

除此之外,ldap_connect应该像ldap://ldap.example.com:1234ldaps://192.168.1.1:4567一样使用包含LDAP-URI的单个参数来调用。停止将它与两个参数hostnameport一起使用,因为这已被弃用,并且可能在PHP的后续版本中被删除,因为没有办法将其用于安全连接。总是叫它ldap_connect('ldap://ldap.example.com:1234'); !