繁体   English   中英

如何使用php识别Windows LDAP和Linux LDAP?

[英]How can I identify windows LDAP and linux LDAP using php?

我需要使用php连接到ldap服务器,但不幸的是我无法使用ldap_bind()方法将其绑定,因为ldap_connect()始终返回true。

我正在使用php.net上的示例脚本

我怀疑ldap主机是否是Windows。 如何通过代码识别?

<?php
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connection

echo "<h3>LDAP query test</h3>";
echo "Connecting ...";
$ds=ldap_connect("localhost");  // must be a valid LDAP server!
echo "connect result is " . $ds . "<br />";

if ($ds) { 
    echo "Binding ..."; 
    $r=ldap_bind($ds);     // this is an "anonymous" bind, typically
                           // read-only access
    echo "Bind result is " . $r . "<br />";

    echo "Searching for (sn=S*) ...";

    // Search surname entry
    $sr=ldap_search($ds, "o=My Company, c=US", "sn=S*");  
    echo "Search result is " . $sr . "<br />";

    echo "Number of entries returned is " . ldap_count_entries($ds, $sr) . "<br />";

    echo "Getting entries ...<p>";
    $info = ldap_get_entries($ds, $sr);
    echo "Data for " . $info["count"] . " items returned:<p>";

    for ($i=0; $i<$info["count"]; $i++) {
        echo "dn is: " . $info[$i]["dn"] . "<br />";
        echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
        echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
    }

    echo "Closing connection";
    ldap_close($ds);

} else {
    echo "<h4>Unable to connect to LDAP server</h4>";
}
?>

我之前遇到过这个问题。也许是因为版本

ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

我用这个来解决它,你可以尝试。

如果要确定操作系统是否为Windows,则可以使用php常量PHP_OS

这是一个小例子,函数isWindows()样子:

function isWindows() {
    if( substr( strtolower( PHP_OS ), 0, 3 ) === 'win' ) {
        return true;
    }

    return false;
}

var_dump( isWindows() );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM