Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to MSSQL server in PHP using integrated authentication

I have XAMPP running on an MS Server 2012 R2. I need to make a connection to an MSSQL server which accepts only integrated win authentication.
If I simply try the below, I get a login failure and an error log on the SQL Server that SQL authentication is not an option. It only accepts connection from a certain user. Obviously the PHP script is not being run under that account.

$server = "sqlServerName";
$SQLUser = "username";
$SQLPass = "pw";
$SQLDatabase = "db";
$link = mssql_connect($server,$SQLUser,$SQLPass);

As I'm using PHP 5.3.1 sqlsrv_connect is not an option. I tried to load the php drivers for it but it's just not working. I can't change the authentication for the sql server and I can't use any other version of PHP.

I also can't turn the secure_connection on as I have to be able to connect to other sql servers which requires "normal" sql authentication:

mssql.secure_connection = Off

How to connect to my problematic sql server?

UPDATE: Upgraded xampp to the latest version. PHP is now version 5.6.8 I still can't use sqlsrv_connect() even though I installed the necessary driver and added every single dll to the php.ini. Restarted apache several times. Any clue?

error msg: Fatal error: Call to undefined function sqlsrv_connect()

like image 725
fishmong3r Avatar asked Jun 10 '15 14:06

fishmong3r


People also ask

How do I connect to SQL Server with SQL authentication?

On the Security page, under Server authentication, select the new server authentication mode, and then click OK. In the SQL Server Management Studio dialog box, click OK to acknowledge the requirement to restart SQL Server. In Object Explorer, right-click your server, and then click Restart.

How connect MS SQL with PHP?

Running MS SQL Queries from PHP See the full list of MS SQL functions in PHP at php.net. The following script is an example of an MS SQL SELECT query run from PHP. <? php $query ="SELECT col1,col2 FROM tablename"; $result =mssql_query($query); while ( $record = mssql_fetch_array($result) ) { echo $record["col1"] .

Does SQL database support Windows integrated authentication?

The Microsoft ODBC Driver for SQL Server on Linux and macOS supports connections that use Kerberos integrated authentication.

What is Integrated authentication in SQL Server?

Integrated Windows Authentication (IWA) refers to a set of authentication protocols that are used by Windows clients and servers. You can use IWA with IBM® Integration Bus to provide transport-level security when you are recording data with a Microsoft SQL Server database.


1 Answers

Ok. It's hard to debug a server issue without being on the server but I've done a lot with php and SQL Server so I will do my best to share my experiences.

First, Very glad you updated from 5.3.1 that version of php is ancient and very insecure. Here are some sanity checks for your system. This may do nothing for you but all of it is worth checking.

  1. First make sure you can connect to sql server using SQL Server Management studio with the credentials you provided. This means the same credentials you use in php not the windows authentication credentials. You should be able to have both connections at the same time so you can make changes and test the connection at the same time.

    1. Enable tcp. sql server configuration manager -> SQL Server network configuration -> protocols for sqlexpress -> tcp/ip (right click)-> properties -> Enabled (yes) -> ip Addresses -> IPAll -> TCP Port 1433 -> ok
    2. Enable sql server auth. Select server (right click) -> properties -> security -> sql server and windows authentication mode -> ok
    3. Open sql server port on firewall. Windows Control panel -> system and security -> windows firewall -> advanced settings -> Inbound rules -> New rule -> Port -> tcp -> 1433 (or whatever) -> Allow connection -> next -> Name -> sql server -> finish -> restart computer.
    4. Of course if you want to connect through a non-default user you need to add the user: sql server -> security -> logins (right click) -> add login -> server roles -> sysadmin -> ok
    5. If you make any of these changes restart sql server: Sql server configuration manager -> sql server services -> sql Server (right click) -> restart.
  2. Once you confirm you can connect with management studio here are the php configuration checks:

    1. You can see if the extension itself is available by creating a php page with only the function phpinfo() in it. Then search for pdo_sqlsrv. If it is present the rest of these checks are probably not necessary but since you've been working this so long probably check them anyway.
    2. sql_srv extension for php should be version 3.2 for php 5.6 you can obtain that library here
    3. Version 3.2 requires an os extension available here Check the other requirements on the previous link. Your os may use a different extension from the one linked here.
    4. Find your php extensions directory. this is usually {php-install-directory}/ext. Make sure you copy the appropriate version of the downloaded sqlsrv libraries into this directory. Mine are called "php_sqlsrv_55_ts.dll" and "php_dpo_sqlsrv_55_ts.dll" Yours will have 56 instead of 55 I think and the "ts" should match your php install. "ts" means thread safe, the other option is "nts" not thread safe. The one you use is dependent on your php install.
    5. My php.ini file contains these lines extension=php_sqlsrv_55_ts.dll and extension=php_pdo_sqlsrv_55_ts.dll in that order. but I don't think order matters. and again yours will be 56 and the "ts" may be "nts".
    6. If you made any changes based on these make sure to restart apache then check if pdo_sqlsrv is in your phpinfo() report. Also after restarting apache check the apache and php error log to see if you get specific errors about php trying to load the extensions. Post those here if you need help with them.
  3. Once you are connected to sql server through the auth creditionals in management studio and see pdo_sqlsrv in your phpinfo() here are the last things to look into in your code.

    1. Your code above is still for mssql extension. You probably just didn't update it with your latest changes. For sql server extension your code should look like this:

      $connectionInfo = array( 'UID' => $dbuser, 'PWD' => $dbpass, 'LoginTimeout' => 1, ); $host = $host . ', ' . $port; $connection = sqlsrv_connect($host, $connectionInfo); $error_messages = sqlsrv_errors();

    2. For windows authentication exclude the uid and pwd.

      $connectionInfo = array(); $conn = sqlsrv_connect( $host, $connectionInfo);

If you have more issues please tell me which step is not working so we can dig into more detail with that step.

like image 105
danielson317 Avatar answered Oct 10 '22 09:10

danielson317