Connecting to MS SQL Server 2000 using PHP with Windows Credentials - php

I know this is not a new question because i found lots of similar questions but not the one which solves my issue.
Well my question is how do i connect to a remote sql 2000 server using other domain windows credentials (not the one im logged in with) with php running on apache (xampp)... and the server doesnt run named instance.
These the things i tried,
installed SQL Native client for my php 5.3
got the .dll files to the ext folder
made sure i got the sqlsrv up and running via phpinfo
mssql.secureconnections is on
edited the php.ini and restart the apache server
So, i tried looking at various posts here and elsewhere but couldnt actually solve my problem.
i tried several codes, but here is the one which im currently trying with
$server = "XXX_Server";
$connInfo = array( "Database"=>"DB_Server", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($server, $connInfo);
if( $conn )
{
echo "Connection established.";
}
else
{
echo "Connection could not be established.";
die( print_r( sqlsrv_errors(), true));
}
and i get error as shown below,
Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. ) )
where xx = domain
And please note that , the client has given me a windows credentials which has access to the server and must use only this. And when i hardcoded my credentials (which has access too) in the script, it didnt work and got the error above, but when i use QueryExpress / SQL Studio, im able to access the db using windows credentials (mine).
So to make it simple, i need to write a script where i need to hardcode the windows credentials and connect to a SQL DB (Stuck here) and make the script accessible for only few using windows ad authentication (which i know how to do)
Please provide me some tips and advice as im stuck and dont know how to proceed. Whatever i research and try, i end up doing the same thing, facing a wall :P
Thanks in advance

Related

Connecting to SQL Server with sqlsrv_connect using Windows authentication

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?
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.

Intermitant error Named Pipes Provider: Could not open a connection to SQL Server[53]

I've searched through the Internet with respect to this error and I've not found anyone who is having this problem on an intermittent basis. It seems my application will run for an extended period of time with no problem, then all of a sudden it stops working and I get this error. Then after some time(I have no idea how long this is) the application will start working again. But lately to correct the problem I just restart the IIS server. Could there be a problem with the application pool? There are no errors in any of my event logs.
My server configurations are as follows Windows 2008 64-bit running PHP 5.4. I'm connecting to a SQL Server 2005 on Windows 2003.
Here is my error.
Thanks for any help
Connection could not be established.
Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 53 [code] => 53 [2] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. [message] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired [message] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 53 [code] => 53 [2] => [Microsoft][SQL Server Native Client 11.0]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][SQL Server Native Client 11.0]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. ) )
Is it possible that your Server's IP address lease expires, get's renewed but the DNS record does not get refreshed? It sounds like your server does allow remote connections but for whatever reason, it looks like it is not being found.
I think I solved my problem. I changed my new Web server to only use my internal DNS. I also entered values into my WINS settings for the web server. The error has not occurred since. I hope this helps anybody else who is having the same problem.
I have faced this problem many times and found that this happens because of improper DNS configuration at server level. Hence, namedpipe fails. The solution i discovered is to create odbc (# client) using tcp/ip address instead of domain name of sqlserver.
Named database (default catalog) does not exist on the specified server (host). I get this error on attempt to connect to a linux environment that has no SqlServer at all.

PHP: Connect to MS SQL Server using Domain user and Password

I have WAMP and am trying to connect to to my SQL Server 2008 database by entering a domain user and password using the following PHP command:
$serverName = "MySQLServer\Instance"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName",
"UID"=>"myADUserName", "PWD"=>"MyPassword");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
As I understand it, it is trying to authenticate with a SQL Server created user and not from the Active Directory, so I am getting the following error message:
Array ( [0] =>
Array ( [0] => 28000 [SQLSTATE] => 28000
[1] => 18456 [code] => 18456
[2] => [Microsoft][SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
[message] => [Microsoft]
[SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
)
[1] =>
Array ( [0] => 28000 [SQLSTATE] => 28000
[1] => 18456 [code] => 18456
[2] => [Microsoft][SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
[message] => [Microsoft]
[SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
)
)
Is this possible in PHP with WAMP?
I've read about changing the log on account in the wampapache service to myADUserName, but the user doesn't have access administrator access on the server to allow this and I wouldn't want to tie one account to all my PHP applications.
When you assign "UID"=>"myADUserName", "PWD"=>"MyPassword" you are using sql-authentication and not Windows authentication.
But you can create that user/pwd combination into the sql-server as sql-login -> SQL Server Authentication.
When you are want to use windows authentication you don't need to specify uid and pwd. the authentication is taken from the running apache process. Of curse that one needs to be run into that windows account.
See Microsoft documentation
Remarks If values for the UID and PWD keys are not specified in the
optional $connectionInfo parameter, the connection will be attempted
using Windows Authentication. For more information about connecting to
the server, see How to: Connect Using Windows Authentication and How
to: Connect Using SQL Server Authentication.
It looks strange, but this one worked for me,
$dbName='dB-Name';
$dbUser='User1';
$dbPass='B4X345ZXsaoi0omkLH';
$connectionInfo = array( "Database"=>$dbName, "UID"=>$dbUser, "PWD"=>$dbPass);
If you are using SQLSERVER this wont work with php 5.5(I'm using this version didn't test any other)
$connectionInfo = array( "Database"=>"DBName", "UID"=>"User1", "PWD"=>"B4X345ZXsaoi0kLH");

Connecting to SQL Server 2012 on IIS8 with PHP_SQLSRV

This is a project, so doesn't require extreme levels of caution/security - I'd like to get it working and then improve it from there.
How can I get PHP to connect successfully to SQL Server 2012
What I have done successfully:
Enabled mixed authentication on SQL Server 2012
Established an account using SQL authentication
Set up FastCGI on IIS and configured PHP.ini
.PHP scripts in the web root execute successfully
I can use SSMS to connect locally to the SQL database using the credentials (SQL Authentication) I created
The problem:
I cannot use a PHP script to successfully connect to the local SQL database.
What I have considered, but do not understand well enough to diagnose:
That the HTTP connections to IIS are impersonating IUSR, which has permissions to read/execute PHP scripts in the web root, but may not have permission to connect to the server?
That for some reason my connection dialogue is simply ineffective
Here is my connect script:
$serverName = 'localhost';
$connectionInfo = array( 'Database'=>'DATABASE',
'Encrypt'=>'1', 'UID'=>'user', 'PWD'=>'password' );
$connection = sqlsrv_connect($serverName, $connectionInfo);
if( $connection == false )
{
echo "No connection was established.\n";
die( print_r( sqlsrv_errors(), true));
}
Here are my errors (unfortunately PHP does not pretty print them):
No connection was established. Array (
[0] => Array (
[SQLSTATE] => 08001
[code] => -2146893019
[message] => [Microsoft][SQL Server Native Client 11.0]SSL Provider: The certificate chain was issued by an authority that is not trusted.
)
[1] => Array (
[SQLSTATE] => 08001
[code] => -2146893019
[message] => [Microsoft][SQL Server Native Client 11.0]Client unable to establish connection
)
)
I also tried using process monitor to check w3wp.exe when I load the script through a browser - I don't know how I would paste the information from that, but there doesn't seem to be anything wrong with its output.
The error message is straight-forward: the SSL certificate you're using on your SQL Server is self-signed and so is not trusted by the consumer.
Possible solutions include:
Disable SSL/TLS on your SQL Server
Use a non-secured connection to your SQL Server
Add the self-signed certificate to the consumer's trusted certificate store
Get a trusted certificate from a public CA (Namecheap do them for less then $20)

Connecting to SQL Server 2008 through PHP

I need to connect to a SQL Server 2008 through PHP (WAMP, latest version). I have the sqlsrv drivers installed and set up and they do show up in phpinfo().
I'm using the following lines to connect to my DB, using Windows Authentication:
$serverName = "(local)";
$connectionOptions = array("Database"=>"MyTestDatabase");
$conn = sqlsrv_connect( $serverName, $connectionOptions) or die("Error!");
And I'm getting the following error:
Array
(
[0] => Array
(
[0] => IMSSP
[SQLSTATE] => IMSSP
[1] => -49
[code] => -49
[2] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
[message] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
)
[1] => Array
(
[0] => IM002
[SQLSTATE] => IM002
[1] => 0
[code] => 0
[2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
[message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
)
)
Any help would be great, but please be specific since I really don't know my way around WAMP except for a few basics.
The error is caused by a permission issue in the registry. There is a key where the path to the native client is stored and the identity you are using in the application pool is getting denied access.
Download Process Monitor and follow the instructions on this post:
http://www.iislogs.com/articles/processmonitorw3wp/ (This tutorial shows how to do it on IIS, with WAMP you will need to find the .exe process that runs on memory)
Find the registry key where the process w3wp.exe is being denied access. That in the case of IIS, not sure what's the name of the process in WAMP but the procedure is the same.
In my case:
HKLM\Software\ODBC\ODBCINST.INI\SQL Native Client 10.0
Run regedit and find the key
Right click and go to Permissions
Add Network Service to the list and give it Read permissions.
Recycle the app pool and it should work
sqlsrv_connect PHP manual
Try this approach to see what the error actually is:
$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));
}
Based on your setup, you may need to revert to the mssql_connect and corresponding functions. This works fine with mssql 2008 and you don't usually lose meaningful functionality.
Here's an example of setting up a connection to the database. Depending on your configuration you may have to add port information, etc.
$hostname = "server.domain.com";
$database = "DatabaseName";
$username = "LocalSQLUserName";
$password = "P#ssw0rd";
$Connection = mssql_connect($hostname, $username, $password);
If you are running on a XAMP environment, then it would stand to reason that you are running everything locally. If that's the case, your server would be localhost or 127.0.0.1 and you can define your own accounts in SQL. As I said, I don't use Windows accounts myself, but you can add an NT account to your "server" and then in SQL Server Management tool set that user to have access to the DB you are using. Then you have set the account and password and know what they are. If you have trouble with the user account, you can try computerName\userName.
If you are not the admin, you'll need to get the relevant information from them.

Categories