1
votes

I can remote access in using the admin username and password which I'm using the same username and password in the script. but I get the following error

Warning: ldap_bind(): Unable to bind to server: Can't contact LDAP server in

Could not bind to the server. Check the username/password. Server Response: Error Number: -1 Description: Can't contact LDAP server

<?php
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);

     // Username used to connect to the server
     $username = "Administrator";

     // Password of the user.
     $password = "password";

     // Either an IP or a domain.
     $ldap_server = "10.10.10.10";

     // Get a connection
     $ldap_conn = ldap_connect($ldap_server);

     // Set LDAP_OPT_PROTOCOL_VERSION to 3
     ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3) or die ("Could not set LDAP Protocol version");

     // Authenticate the user and link the resource_id with
     // the authentication.
     if($ldapbind = ldap_bind($ldap_conn, $username, $password) == true)
     {
     // Setup the data that will be used to create the user
     // This is in the form of a multi-dimensional
     // array that will be passed to AD to insert.
     $adduserAD["cn"] = "testuser";
     $adduserAD["sn"] = "User";
     $adduserAD["samaccountname"] = "testuser";
     $adduserAD["objectClass"] = "user";
     $adduserAD["displayname"] = "Test User";
     $adduserAD["userPassword"] = "Welcome123!";
     $adduserAD["userAccountControl"] = 544;

     $base_dn = "cn=testuser,cn=Users,DC=testdomain,DC=com";

     // Display some "waiting" text.
     echo "Trying to add the user to the system ...<br>";

     // Attempt to add the user with ldap_add()
     if(ldap_add($ldap_conn, $base_dn, $adduserAD) == true)
     {

     // The user is added and should be ready to be logged
     // in to the domain.
     echo "User added!<br>";
     }else{

     // This error message will be displayed if the user
     // was not able to be added to the AD structure.
     echo "Sorry, the user was not added.<br>Error Number: ";
     echo ldap_errno($ldap_conn) . "<br />Error Description: ";
     echo ldap_error($ldap_conn) . "<br />";
     }
     }else{
     echo "Could not bind to the server. Check the username/password.<br />";
     echo "Server Response:"

     // Error number.
     . "<br />Error Number: " . ldap_errno($ldap_conn)

     // Error description.
     . "<br />Description: " . ldap_error($ldap_conn);
     }

     // Always make sure you close the server after
     // your script is finished.
     ldap_close($ldap_conn);
    ?> 
2
Can you ping the server? Have you tried an LDAP browser to verify connecting outside of PHP? What have you done so far? - mikeb
I can ping the server, and connect and run other php scripts just fine. I've made sure that ldap is enabled in my php manager on IIS 7.5. - mike peterson
Can you run a basic ldapsearch on the command line and what is the output ? You can ping the server but can you telnet on the target port ? WHich port ? By the way you are using ldap_connect($ldap_server); without specifying $port so the connection defaults with ldap:// protocol on port 389. Your server may require ldaps:// protocol (usually on port 636), or even StartTLS over ldap:// on port 389 (-Z option on cmd line), anyway if you need SSL/TLS, check that you have a copy of the CA certificate on your client machines. - EricLavault

2 Answers

-2
votes

You should check the result of your first ldap_connect(...) call.

And:

Try something like this or this to validate all of your connection information. This has been very helpful to me to make sure you have the right account information and LDAP directory information.

-2
votes

why following line doesn't have port in it?

$ldap_server = "10.10.10.10";
$ldap_conn = ldap_connect($ldap_server);

try something like below

 if($authMethod == 0){
    $ldap_server = 'ldap://'.$host.':'.$port;
}else if($authMethod == 1) {
    $ldap_server = 'ldaps://'.$host.':'.$port;
}
$ldap_conn = ldap_connect($ldap_server);

and then check $ldap_conn is true or false or any other return code. port#389 is default for simple LDAP and 636 for LDAPS. I hope this should work :). Cheers!!!!