1

The official guide for connecting to SQL Server using the PHP driver from Microsoft seems pretty straightforward, so I copied the text for the most part:

$server = "serverlocation\prod";
$database = array( "Database"=>"temp");
$conn = sqlsrv_connect($server, $database);

echo "<pre>";

if ( $conn ) {
  echo "Connection established";
} else { 
  echo "Connection could not be established.<br />";
  die( print_r( sqlsrv_errors(), true));
}

When I run the code, I got the following error:

Connection could not be established.
Array
(
    [0] => Array
        (
            [0] => 28000
            [SQLSTATE] => 28000
            [1] => 18456
            [code] => 18456
            [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user '*****DOMAIN\MY_MACHINE_NAME*****'.
            [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user '*****DOMAIN\MY_MACHINE_NAME*****'.
        )

I am running this code on the machine name that is listed in the error message, which is a Windows machine, but the machine name listed is NOT the user account under which I am logged in. The user account under which I am logged in has rights to this database, (and I can connect to it using MS SQL Server Managment Studio just fine...) but when I try to connect using this script, it displays my machine name instead of my user account.

What am I doing wrong?

2
  • I don't know a lot about PHP server. Is it a module you install in IIS or is it standalone? Basically its running as the web server account, not as you. Here's a bit of a discussion: serverfault.com/questions/105420/… Commented Jun 8, 2017 at 23:48
  • 1
    As per Nick's comment, if you read the first paragraph of the Microsoft doc you linked to in the question it says " It is important to notice that in most scenarios, this means that the Web server's process identity or thread identity (if the Web server is using impersonation) is used to connect to the server, not an end-user's identity." So it appears the web server is using the machine account to connect to the database. Commented Jun 9, 2017 at 0:19

1 Answer 1

1

Assuming that you're running this over the web:

  • You should be using IIS and not Apache
  • IIS must have Windows Authentication enabled
  • Anonymous authentication must be disabled
  • You must set fastcgi.impersonate = 1 in your php.ini

Assuming that the account you're logged in as gets recognized by SQL Server, you should be good to go. See this page for a complete discussion.

In the comments for that post, the author suggests that this may also be possible with Apache.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.