I am trying to connect to a SQL server database with a windows account. In sql managment studio, I can open the database with this account but not with my php application. This user is not a sql user, but he have the right on the SQL database.
I tried with a sql user and it works with my php application.
Is it possible to use my windows account from the php application, and not a specifique sql user?
sqlsrv_connect
By Default it trys to establish a Connection with a Windows Auth. u can try the snipped below. If u have to use a defined WindowsUser to Login, try the Code from the documentation link above.
$serverName = "serverName\sqlexpress"; //serverName\instanceName
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"dbName");
$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));
}
There is a part of code with connection string in which you provide instance name, login, password etc for PHP to connect to MS SQL. You can change connection string on your credentials from windows domain. But its acceptable for testing but not for production :)
Another way is to connect PHP to your AD and make user to enter login/pass from domain when they come on your site. For example it is simply done in IIS (I haven't work with domain auth on PHP that runs on Linux, but as I know LDAP can help you).
Related
Actually I have try to connect SQL server by using sqlsrv_connect and using the DSN (Data Source Name) without Apache service then the both are perfectly working.
The problem is when I turn on the apache service then php cannot connect to the sql server using odbc DSN (Working with sqlsrcv_connect).
The condition is I need to turn on the apache with a application running using sql server which using DSN. I working with crystal report thats why really need this method. I have tried using system DSN instead user dsn, its also not working.
!
I wrote code below to test my scenario
// Connect to the data source
$conn=odbc_connect('DSNNAME','DBUSER','DBPASS');
if ($conn){
echo "Connection established DSN";
}
else {
echo "Connection using DSN Failed:" . odbc_errormsg();
}
// Connect through server name
$serverName = "WEBSERVER\SQLEXPRESS"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"DBUSER", "PWD"=>"DBPASS");
$conn2 = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn2 ) {
echo "Connection established using server name";
}else{
echo "Connection could not be established by using server name";
die( print_r( sqlsrv_errors(), true));
}
The Result when Apache service is on.
Make sure the ODBC DSN matches the Crystal runtime in terms of 32-bit or 64-bit.
TLDR: I'd like to connect a php application to a MS SQL server through an active directory account but haven't had much luck finding documentation on how to do that.
Most of the issues that people seem to have with this are pretty complicated I think I just don't have a strong enough grasp of the concept. When I try to connect it to the server, I get:
Warning: mysql_connect(): No connection could be made because the target machine actively refused it.
The code I'm using is:
$user_name = "ADAccountName";
$pass_word = "ADAccountPW";
$datbase = "myDatabase";
$server = "#ipaddress#:#port#";
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
I created a user "sa" through MS SQL Server Management Studio and attached it to an AD account (I don't seem to have access to create one without picking an AD account).
I've had luck connecting it to a database using MySQL Workbench (for testing) and a dedicated username/password, but the actual database I need to use is run by MS SQL Server Manager with an AD account. I've spent the greater part of the day going through forums and Microsoft's documentation on SQL Server Manager but I haven't found anything that can help me out with this.
I'm using php v. 5.2
Depending which version of PHP you are using you could do the following
resource mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]] )
URL - http://php.net/manual/en/function.mssql-connect.php
This would give you direct connection to the DB.
If you're trying to connect to MSSQL you will need to do a few things to your web server first.
1.You will need to add PHP to your windows startup
2.Then you will need to install SQL Server Native client
3.Install the SQL Server drivers for your web server. SQLSRV30 is for PHP 5.4, SQLSRV31 is for PHP 5.5
4.Then go to your php.ini file and enable these extensions.
This will enable you to use PHP Data Objects.
You can connect to the DB like this:
try
{
$conn = new PDO( "sqlsrv:server=$serverName ; Database=AdventureWorks", "sa", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die( print_r( $e->getMessage() ) );
}
I'm trying to connect to a remote ms sql db (not a localhost), but everytime it timeouts before it sucseeds...
I'm pretty sure that the problem is the $serverName variable, is there anyway to check via Plesk Parallels what is the value of that?
<?php
$serverName = "server's ip address/database name"; //serverName\instanceName
$connectionInfo = array( "Database"=>"database name", "UID"=>"DBusername", "PWD"=>"DBpassword");
$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));
}
?>
Try to inject precise port number into ServerName like
<?php
$serverName=127.0.0.1\SQLEXPRESS,1433; //networkAddr\InstanceName,<portNum>
?>
Try using Ip address instead NetBios name if you do, cause ICMP protocol is usually blocked due security policy.
Also, the default port 1433 may be blocked. Should contact server administrator to provide actual connection requisits in that case.
The problem was that my ip address was not permitted to connect to the database, after I contancted my hosting service provider they gave me that permission and it succeeded.
I am trying to establish a connection to my company's MSSQL server with windows authentication , but it fails as it is trying to use my computer name as login instead of my login id. When logging in with the MS SQL Server Management the windows authentication works fine, but not with this PHP code:
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$serverName = "xxx";
$connectionInfo = array( "Database"=>"xxx");
/* Connect using Windows Authentication. */
$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));
}
//
?>
The printout I get is the following:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for
user 'xxx\T2002197$'. ) )
T2002197 is my computer name, not my login id, so of course it fails. How can I solve this? I am using WAMP.
Edited out some info, replaced with 'xxx'
Aah problem solved! I changed the settings on my WAMP service (open service.msc in Windows) and made sure the service logged on the correct account. It works now.
The manual says that per default the connection is using Windows Authentication and not the SQL Server authentication.
To bypass this you need to provide the uid and pwd options in your $connectionInfo data.
Se the manuals;
http://php.net/sqlsrv_connect
http://msdn.microsoft.com/en-us/library/ff628167.aspx
I am attempting to connect to a Microsoft SQL server db using PHP (in IIS). The user/pass provided in the code is an AD account that can access the database via ODBC in Access just fine.
$serverName = "servername";
$connectionInfo = array( "Database"=>"dbname", "UID"=>"myuser", "PWD"=>"mypass");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br /><pre>";
die( print_r( sqlsrv_errors(), true));
}
The username and password are valid, as are the server name and database name. But this is returning the error:
[Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'myuser'.
For the username (which is a valid user in AD, and I can connect/link to this database using Access) I've also tried:
DOMAIN.ORG\myuser
DOMAIN.ORG\\myuser
myuser#DOMAIN.ORG
My domain user (myuser) has admin privileges to that server via AD. But that returns the same Login failed error. Is there something I'm missing? I can connect to this same db using Access without a problem (under the same user).
The way I connect in Access is an ODBC Data Source for the local user. The Data Source is set to authenticate "With Windows NT authentication using the network login ID". How can I do that in PHP?
Is this impersonation configuration the possible cause?
PHP is running under the user nt authority\iusr
If it was trying to login using iusr wouldn't the error message say so?
I've tried creating a Data Source on this machine called "TestDS" and using the code:
$conn = odbc_connect("TestDS", "", "");
But that gives me:
The specified DSN contains an architecture mismatch between the Driver and Application, SQL state IM014 in SQLConnect
Machine1 is a client trying to access a webpage (where the PHP code is on Machine2) and the code on Machine2 is making the SQL connection to Machine3 (servername).
"Windows Security" in SQL Server connections is enabled by specifying "Trusted Connection=SSPI" in the connection string. No username or password is stored in the connection string (that's only for "SQL Server Security"). The process running PHP (presumably php_cgi.exe itself) must run under a Windows (either local or AD) that has a Windows login registered on the SQL Server.
Under IIS php_cgi.exe runs under the user identity of the application pool. If you have a heterogenous security environment I suggest creating an IIS application pool just for this single website.