Unable to connect SQL Server from PHP using Windows Authentication - php

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!

Related

Connection Could Not be Established with SQL Server 2019

I'm having a problem running my PHP project on our IIS with SQL Server 2019. My version of PHP is 7.0.26 and I have already imported SQLSRV DLLS.
When I try to login with my system, the following error is popping up:
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 'sa'. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'sa'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'sa'. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'sa'. ) )
This is some part of my connection string.
$myCon = array("Database"=>$myDB, "UID"=>$myUser, "PWD"=>$myPass);
$db = sqlsrv_connect( $myServer, $myCon);
Also, I tried to trace the error, like adding the wrong credentials to see if there is some changes on the error message but it is still the same.
We tried some configurations, like setting up the server authentication into "SQL Server and Windows Authentication Mode", firewall is disabled, and also setting up the Native client configuration.
IIS configuration was made setting MIME Type, default document, connection string, Application Pool, and all the path config was set.
I also installed only ODBC Driver 13, 2015 Redistributable, .NET 4 framework.
Can anyone help me? Thank you.
The SA account is created during the installation process. The SA account is well known and often targeted by malicious users, so it is advisable to disable the sa account unless your application requires it. you could create a new account in the SQL server or use windows authentication for accessing the SQL database.
<?php
$serverName = "serverName\\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$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));
}
?>
Refer below link for more detail on how to create a user and configure with iis:
https://forums.iis.net/post/2159167.aspx
https://learn.microsoft.com/en-us/sql/connect/php/programming-guide-for-php-sql-driver?view=sql-server-ver15
https://learn.microsoft.com/en-us/sql/connect/php/connecting-to-the-server?view=sql-server-ver15

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

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));
}
?>

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

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.

Can't Establish connection to MS SQL Server 2008

I have been trying to establish a connection to a SQL Server 2008 but i get a return message saying:
Connection could not be established. Array ( [0] => Array ( [0] =>
08001 [SQLSTATE] => 08001 [1] => -1 [code] => -1 [2] =>
[Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network
Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
[message] => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server
Network Interfaces: Error Locating Server/Instance Specified
[xFFFFFFFF]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] =>
0 [code] => 0 [2] => [Microsoft][ODBC Driver 11 for SQL Server]Login
timeout expired [message] => [Microsoft][ODBC Driver 11 for SQL
Server]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE]
=> 08001 [1] => -1 [code] => -1 [2] => [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred
while establishing a connection to SQL Server. Server is not found or
not accessible. Check if instance name is correct and if SQL Server is
configured to allow remote connections. For more information see SQL
Server Books Online. [message] => [Microsoft][ODBC Driver 11 for SQL
Server]A network-related or instance-specific error has occurred while
establishing a connection to SQL Server. Server is not found or not
accessible. Check if instance name is correct and if SQL Server is
configured to allow remote connections. For more information see SQL
Server Books Online. )
My PHP Code:
<?php
$serverName = "SERV002\SQLEXPRESS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"YSHSDB", "UID"=>"sa", "PWD"=>"password");
$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));
}
?>
UPDATE: I have fixed this by installing xampp lite onto my server 2003 running the SQL server however, I would still like to be able to access the SQL server if the web services is running on another machine.
The obvious error here is that your SQL server is not running or is not configured to allow remote connections (I think that is the default for SqlExpress)
You will need to open your Sql Connection Manager and change the settings for TCP connections to make them available for remote connections. You may also have a firewall blocking your Sql Connection somewhere. Make sure port 1433 is exposed in any fire wall.
Open your Server Configuration Manager
Server Configuration Manager
Under SQL Server Network Configuration
Click on Protocols for SQLEXPRESS
Rightclick and access Properties of TCP/IP
Select the IP Addresses Tab
Now configure all the TCP Port(s) to the port you are using in your connection string.

Categories