IIS PHP using Windows Authentication unable to connect to SQL Server 2008R2 - php

I'm running a PHP site using IIS and connecting to a Sql Server server on another computer on the network. I created a domain user that has access to the db and configured the app pool to use that domain user.
I keep getting this error:
Unable to connect.
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 'NT AUTHORITY\ANONYMOUS LOGON'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
)
[1] => Array
(
[0] => 28000
[SQLSTATE] => 28000
[1] => 18456
[code] => 18456
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
)
)
Not sure why it's using user 'NT AUTHORITY\ANONYMOUS LOGON' instead of the domain user in the application pool.
Thanks

I got this working by clicking on Websites in IIS, then double click on Authentication, and right click on Anonymous Authentication->Edit, and select the Application Pool Identity.

1.) Install System Internals Process Monitor, if not already installed.
https://technet.microsoft.com/en-us/sysinternals/bb842062
2.) Check the monitoring logs to see exactly where the anonymous login is occurring.
That will allow you to diagnose exactly what's going on.

Related

Unable to connect SQL Server from PHP using Windows Authentication

I'm trying to connect to MS-SQL Server from PHP-8.0 (hosting on IIS-10 localhost) using SQL Server Driver for PHP, i.e., SQLSRV-5.9. While connecting from a PHP page, if I use SQL Server Authentication, it's getting connected right away. But while using Windows Authentication, it's throwing an error in the browser saying:
Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456
[code] => 18456 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL
Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] =>
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for
user 'NT AUTHORITY\IUSR'. ) [1] => Array ( [0] => 28000 [SQLSTATE] =>
28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 17
for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'.
[message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL
Server]Login failed for user 'NT AUTHORITY\IUSR'. ) )
My environment is Windows 10 Pro, Version 21H2, 64-bit. In my SQL Server, both Windows Authentication and SQL Server Authentication are enabled, and I'm able to connect to the server using both of them through SQL Server Management Studio. So what's stopping my PHP code to connect to the server using Windows Authentication? Both the PHP and SQL Sever are on the same PC. What am I doing wrong? As I'm new in PHP, can you please clarify it with a simple example?
My connect-db.php code follows. Note how I used connection string for both Windows and SQL Server Authentication.
?php
$serverName = "(local)"; // Optionally use port number (1433 by default).
$connectionString = array("Database"=>"TestDB"); // Windows Authentication connection string.
// $connectionString = array("UID"=>"sa","PWD"=>"sa","Database"=>"TestDB"); // SQL Server Authentication connection string.
$conn = sqlsrv_connect($serverName, $connectionString); // Connect to the server.
if($conn === false) {
echo "Connection could not be established.<br/>";
die(print_r(sqlsrv_errors(), true));
} else {
echo "Connection established successfuly.<br/>";
}
sqlsrv_close($conn); // Close connection resources.
?>
Thanks and Regards!

SQL Server 2017 - "Login failed for user" PHP on VirtualBox

I wanna to connect with my database which is hosted on Virtual Machine (VB with Win 8.1). Connection attempt tested by Microsoft Data Link on main machine, returns successful connection but my localhost always display a message:
[0] => 28000
[SQLSTATE] => 28000
[1] => 18456
[code] => 18456
[2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'sa'.
[message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'sa'.
For user with 'Enforce password policy' enabled I got:
[0] => 28000
[SQLSTATE] => 28000
[1] => 18488
[code] => 18488
[2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'userDB2'. Reason: The password of the account must be changed.
[message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'userDB2'. Reason: The password of the account must be changed.
Same code working fine on VM localhost. In both cases, PHP 7.4 is used (with the latest drivers from https://github.com/microsoft/msphpsql).
Server Authentication settings (VM):
Script used:
$server = '127.0.0.1';
$cinfo = array(
"Database"=>'mydbname',
"UID"=>'sa',
"PWD"=>'root'
);
$conn = sqlsrv_connect($server, $cinfo);
if ($conn === false) {
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
Firewall on VM is disabled.
TCP/IP protocol in SQL Server Configuration is enabled.
Port forwarding in VirtualBox settings:
I tried to use different users with different settings but always this errors appears.
UPDATE:
Last SQL Logs

Database Connection error using sql server and codeigniter in IIS

Error to connect MSSQL database through PHP
I can't able to connect to sql server database in remote system. It is showing Unable to connect to your database server using the provided settings.
Filename:
returns the bellow error..
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456
[code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL
Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] =>
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for
user 'NT AUTHORITY\IUSR'. ) [1] => Array ( [0] => 42000 [SQLSTATE] =>
42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 13 for
SQL Server][SQL Server]Cannot open database "pestcontrol" requested by
the login. The login failed. [message] => [Microsoft][ODBC Driver 13
for SQL Server][SQL Server]Cannot open database "pestcontrol"
requested by the login. The login failed. ) )
This can be solve your problem
$db['default']['username'] = 'root';
$db['default']['password'] = '';
It is by default setting in xampp, lampp and wampp. If you want create your own credential you can write it in the code given.
or try this link:
https://www.c-sharpcorner.com/UploadFile/specialhost/how-to-fix-login-failed-for-user-nt-authority-iusrs-in-share/

Wampserver Configuration with SQL Server 2017

Hello to anyone reading this. I am having a serious issue with configuration using Wampserver & SQL Server 2017. The PHP version is 7.1.9. Apache 2.4.27. The inspiration I have received was from a video made a few years ago https://www.youtube.com/watch?v=AUUh_WVzqq4 . All I am trying to do at this point is simply set up the connection to the database locally. I posted my php code and this is the error that I am getting outputted to browser...
Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'WORKGROUP\DESKTOP-REPGNI2$'. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'WORKGROUP\DESKTOP-REPGNI2$'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "officialdatabase" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "officialdatabase" requested by the login. The login failed. ) [2] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'WORKGROUP\DESKTOP-REPGNI2$'. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'WORKGROUP\DESKTOP-REPGNI2$'. ) [3] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "officialdatabase" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "officialdatabase" requested by the login. The login failed. ) )
It seems to be a common error based on what I have been looking up about it. I have tried updating my drivers to the newest version ODBC Driver 13 for SQL Server TO ODBC Driver 17 for SQL Server. I have the dll files in the php.ini located in Apache (I think) and as proof see the phpinfo() screenshot. These dll files are the ones for version 7.0+ for PHP by the way. The service is running under a name "NT Service\MSSQLSERVER" and I have tried changing this to a true user in my database which is just me under the Windows Authentication (In SQL Server my Windows Authentication has privileges as well to the database). I have also tried filling in the "UID" and "PWD" fields in my connectionInfo with a user that I created as an admin. I have tried re-downloading Microsoft Visual C++ 2013 Redistributable (x64) - 12.0.30501. All of this has resulted in no success and the same error. Any idea what the problem could be?
<?php
$serverName = "Desktop-repgni2"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"officialdatabase");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>

On Joomla set up cannot connect to MSSQL due to user name error [duplicate]

This question already has an answer here:
Connecting to SQL Server with sqlsrv_connect using Windows authentication
(1 answer)
Closed 5 years ago.
I installed mssql and it shows enaabled in phpinfo(). But when setting up joomla on database config page when I choose mssql and try to connect the following error comes up
Could not connect to the database. Connector returned number: Database sqlsrv_connect failed, 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 'user'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'user'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'user'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'user'. ) )
Tried putting user root Admin but does not work. How can I get this page working?
Typically, this problem happens when MSSQL server authentication is not enabled in the local server. You can enable that the following way:
Go to MSSQL and then right click
Click on "Properties" and then on "Security"
Click on "Server Authentication"
Enable Authentication Mode

Categories