Php – Warning: ldap_bind(): Unable to bind to server: Invalid credentials PHP and LDAP

ldapldapconnectionPHP

I'm trying to connect to an LDAP server to authenticate user credentials.

I've found a few users with this same issue but their solutions did not work for me.

here's what I'm using:

<?php
define('LDAP_SERVER', 'LDAP://pdc.mydomain.com');
define('LDAP_PORT', 389);
define('LDAP_TOP', 'dc=mydomain,dc=com');
if(isset($_POST['username']))
{
    if(!($ds = ldap_connect(LDAP_SERVER, LDAP_PORT)))
    {
        die ("Could not connect to mydomain domain");
    }
    $un = $_POST['username'].",".LDAP_TOP;
    //echo stripslashes($un)."<br>";
    $ldapbind = ldap_bind($ds, stripslashes($un), $_POST['password']);
    if($ldapbind)
        echo "login success";
    else
        echo "login failed";
}
?>

I've tried using "mydomain\myusername" and just "myusername".

I added the stripslashes() function when neither worked to test that, and still no dice.

the error I get every time is: Warning: ldap_bind(): Unable to bind to server: Invalid credentials

any help would be greatly appreciated

TIA

Best Answer

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.