简体   繁体   English

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

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

I need to connect to an ldap server with php, but unfortunately I am not able to bind it with ldap_bind() method, as ldap_connect() always returns true. 我需要使用php连接到ldap服务器,但不幸的是我无法使用ldap_bind()方法将其绑定,因为ldap_connect()始终返回true。

I am using the example script available at php.net 我正在使用php.net上的示例脚本

I am having doubt that whether the ldap host is windows or not. 我怀疑ldap主机是否是Windows。 How can I identify that by code? 如何通过代码识别?

Code

<?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>";
}
?>

I met the problem before.maybe because of the version 我之前遇到过这个问题。也许是因为版本

ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

I used this to solve it,you can try. 我用这个来解决它,你可以尝试。

if you want to determine whether the OS is windows or not you can use the php constant PHP_OS 如果要确定操作系统是否为Windows,则可以使用php常量PHP_OS

Here is a small example how a function isWindows() could look like: 这是一个小例子,函数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