title: How can I connect to Active Directory using LDAPS on PHP?
body: <p>I was wondering how to connect to my Active Directory Domain Controller using <code>LDAPS</code> in <code>PHP</code> on another windows server. 
  I have exported the root certificate and the server certificate and put the root in my trusted root store and the server authentication in my personal certificates in my windows certificate store. 
  When I try to connect using <code>port 389</code> it's fine, but when I try to connect using <code>port 636</code> I get an error.</p>
  
  <pre><code> // LDAP variables &lt;br&gt;
   $ldap_host = "myhost";   // your ldap servers&lt;br&gt;
   $ldap_port = 636;          // your ldap server's port number&lt;br&gt;
   $base_dn = "OU=Users,OU=domain,DC=example,DC=local";&lt;br&gt;
  
  // Connecting to LDAP&lt;br&gt;
  $connect = ldap_connect( $ldap_host, $ldap_port)&lt;br&gt;
  or exit("&gt;&gt;Could not connect to LDAP server&lt;&lt;");&lt;br&gt;
  echo "Connected to $ldap_host";&lt;br&gt;
  
  ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);&lt;br&gt;
  $ldap_user  = "CN=UserName,OU=No Policy,DC=example,DC=local";&lt;br&gt;
  $ldap_pass = "Password";&lt;br&gt;
  // verify binding&lt;br&gt;
  $bind = ldap_bind($connect, $ldap_user, $ldap_pass)&lt;br&gt;
  or exit("&gt;&gt;Could not bind to $ldap_host&lt;&lt;" . ldap_error($connnect) );&lt;br&gt;
  </code></pre>
  
  <p>The output I get is "Connected to myhost Could not bind to myhost. <br>
  When using port 389 I get "connected to myhost" <br></p>