doukuang6795 2018-01-30 13:01
浏览 265

如何使用PHP7中的Windows身份验证远程连接到SQL Server?

I am struggling to connect to the server in my php application. The server is running and I connect to it via ODBC connection in excel:

DSN=vortest;UID=ramunasc;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=OFFICE22;DATABASE=vordata_sql;ApplicationIntent=READONLY;

I can connect to the server with SQL Server Management Studio and I do so with Windows authentication. However PHP code doesn't work:

$serverName = "MP-SQL2\SQL2008";  
$connectionInfo = array( "Database"=>"vordata_sql", "UID"=>"ramunasc");  
/* Connect using Windows Authentication. */  
$conn = sqlsrv_connect( $serverName, $connectionInfo);  
if( $conn === false )  
{  
    echo "Unable to connect.</br>";  
    die( print_r( sqlsrv_errors(), true));  
}

This gives 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 'ramunasc'.
            [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'ramunasc'.
        )
    [1] => Array
        (
            [0] => 28000
            [SQLSTATE] => 28000
            [1] => 18456
            [code] => 18456
            [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'ramunasc'.
            [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'ramunasc'.
        )
)

I am new Microsoft databases so I am not sure if I am doing something wrong or misunderstanding how these things work.

  • 写回答

1条回答 默认 最新

  • dream_high1026 2018-02-15 13:25
    关注

    Solution:

    1. You must remove "UID"=>"ramunasc" from $connectionInfo. When you set "UID" connection option and not set "Authentication" connection option, the PHP driver tries to use SQL authentication.
    2. It's important to note, that when using windows authentication, the Web server's process identity or thread identity (if the Web server is using impersonation) is used to connect to the server, not an end-user's identity. That's why you can connect to the server with SQL Server Management Studio (authenticated with your windows credentials), but your script fails.

    Test environment:

    1. SQL Server 2005 Express edition, with mixed authentication mode.
    2. Apache 2.4 with PHP 7.1.12 and Microsoft PHP Driver for SQL Server (php_sqlsrv_71_ts_x86.dll, version 4.3).

    Connect with windows authentication (working example):

    <?php
    $server = '127.0.0.1';
    $cinfo = array(
        "Database"=>'master'
    );
    
    $conn = sqlsrv_connect($server, $cinfo);
    if( $conn === false )
    {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    
    $sql = "SELECT CONVERT(varchar(32), SUSER_SNAME())";
    $stmt = sqlsrv_query($conn, $sql);
    if( $stmt === false ) {
        echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    
    $row = sqlsrv_fetch_array($stmt);
    echo "Login name: ".$row[0];
    
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
    ?>
    

    In my case the result is "Login name: NT AUTHORITY\SYSTEM".

    Connect with SQL server authentication (working example):

    <?php
    $server = '127.0.0.1';
    $cinfo = array(
        "Database"=>'master',
        "UID"=>'username',
        "PWD"=>'password'
    );
    
    $conn = sqlsrv_connect($server, $cinfo);
    if( $conn === false )
    {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    
    $sql = "SELECT CONVERT(varchar(32), SUSER_SNAME())";
    $stmt = sqlsrv_query($conn, $sql);
    if( $stmt === false ) {
        echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    
    $row = sqlsrv_fetch_array($stmt);
    echo "Login name: ".$row[0];
    
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
    ?>
    

    The result is "Login name: username"

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)