title: Connect to SQL SERVER 2008 (using Windows Authentication) via PHP in IIS7?
body: <p>I need to connect to Sql Server 2008 using windows authentication from PHP code by passing the username and password like we do when we connect to Sql Server with SQL Authentication. I need to authenticate the user based on Windows Authentication's User/Password .</p>
  
  <p>When I was using SQL Authentication I was able to connect by creating a System DSN named <code>DBconnect</code> in the Data Source (ODBC) configuration tool. Then I could successfully verify the login ID with SQL Server Authentication and connect to the database using Login ID and Password entered by a user with this php code:</p>
  
  <pre><code>$con = odbc_connect("DBconnect", "sql_user", "sql_password");
  </code></pre>
  
  <p>Now I need to do the same thing when SQL Server is using Windows Authentication.
  I want to create a System DSN named <code>DBConnect_NT</code> in Data Source (ODBC) Configuration tool and set it to verify the authenticity of the login ID with Windows Authentication using the network login ID.</p>
  
  <p>I have this code:</p>
  
  <pre><code>$con = odbc_connect("DBconnect_NT", "windows_user", "windows_pass");
  </code></pre>
  
  <p>But it doesn't work. I get this error:</p>
  
  <blockquote>
    <p>Error in query preparation/execution. 28000</p>
  </blockquote>
  
  <p>I changed some settings in the Authentication section of IIS7 Manager</p>
  
  <pre><code>Windows Authentication = Enabled
  Anonymous Authentication = Disabled
  </code></pre>
  
  <p>Now I can use this code:</p>
  
  <pre><code>$con = odbc_connect("DBconnect_NT", "", ""); 
  </code></pre>
  
  <p>and I see a popup prompting me to login. As I type username and password, It connects and hence allows me to do other queries using same <code>$con</code> connection object.</p>
  
  <p>Unfortunately, I'm not able to authenticate the user for the web site based on this approach. When I type wrong username and password to the popup it gives no response (the popup comes again). If I cancel the popup, it will show this 401 error:</p>
  
  <blockquote>
    <p>401 - Unauthorized: Access is denied due to invalid credentials.</p>
  </blockquote>
  
  <p>Am I using correct way to connect to SQL Server via Windows authentication? 
  Is there some other possible way to connect SQL Server by passing the Windows Authentication username and password dynamically?</p>
  
  <p>I have also tried using LDAP to authenticate against Active directory, but its not working.</p>
  
  <p>I'm using SQL SERVER 2008,IIS7 and PHP5.3 in an Amazon EC2 machine.</p>
  
  <p>This is for a flex application I'm building using <code>http://example.net/Webservice/connect.php</code>. Flex dynamically passes the username/password to this popup from a form and hence connects. But when wrong username/password are supplied the popup is difficult to handle. It comes back continuously and the flex application hangs .</p>
  
  <p>If it is possible to receive an error code when the wrong username/password are entered in the popup it may solve the issue. Some other method to connect SQL Server using Windows Authentication and pass the username/password directly would also solve my problem.</p>