简体   繁体   中英

Authenticating with PHP and LDAP with curl

Is there a way to possibly authenticate a user via LDAP but without using the LDAP module of php? Unfortunately most shared hosts do not seem to have ldap module. I have currently got the following working with the ldap module on my localhost but would like to achieve the same thing with an alternative method like cURL or file_get_contents etc.

$ldap_connect = ldap_connect("server.ip", 389);
ldap_set_option($ldap_connect, LDAP_OPT_NETWORK_TIMEOUT, 4);  

        $ldap_fqdn_user=$samaccountname. "@" . $ldap_domain; 

        if (!($bind = ldap_bind($ldap_connect, $ldap_fqdn_user, $ldap_password))) { 
            ldap_close($ldap_connect);
            echo "Incorrect Username or Password"; 
        } else {
            ldap_close($ldap_connect);
            echo "Correct username and password";
        }

fortunately curl has native support for LDAP. i have never used it myself, but i guess the equivalent curl_setopt would be

curl_setopt_array ( $ch, array (
        CURLOPT_PROTOCOLS => CURLPROTO_LDAP,
        CURLOPT_PORT => 389,
        CURLOPT_URL => 'server.ip',
        CURLOPT_TIMEOUT => 4,
        CURLOPT_USERPWD => $ldap_fqdn_user . ':' . $ldap_password 
) );

here's a fully working example, checking if the password is correct/wrong with curl,

<?php
declare(strict_types = 1);
header ( "content-type: text/plain;charset=utf8" );
$ch = curl_init ();
curl_setopt_array ( $ch, array (
        CURLOPT_PROTOCOLS => CURLPROTO_LDAP,
        CURLOPT_PORT => 389,
        CURLOPT_URL => 'ldap.forumsys.com',
        CURLOPT_TIMEOUT => 4,
        CURLOPT_USERPWD => 'cn=read-only-admin,dc=example,dc=com:password' 
) );
if (curl_exec ( $ch )) {
    echo "correct password";
} else {
    echo "maybe wrong password? curl got an error. errno:" . curl_errno ( $ch ) . '. error: ' . curl_error ( $ch );
}
curl_close ( $ch );

change the password to paswordd , for example, and it'll fail. (line 10)

and thanks to http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/ for providing the public test server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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