SQL Server 身份验证只能在无法进行 Windows 身份验证时使用。有关使用 Windows 身份验证进行连接的信息,请参阅如何使用 Windows 身份验证进行连接。 在使用 SQL Server 身份验证连接到 SQL Server 时必须考虑以下几点: 必须对服务器启用 SQL Server 混合模式身份验证。在尝试建立连接时必须设置 UID 和PWD 连接属性。UID 和PWD 必须映射到有效的 SQL Server 用户和密码。注意: 包含右大括号 (}) 的密码必须使用另一个右大括号进行转义。例如,如果 SQL Server 密码为“pass}word”,则 PWD 连接属性的值必须设置为“pass}}word”。在使用 SQL Server 身份验证连接到 SQL Server 时应采取以下预防措施: 保护(加密)通过网络从 Web 服务器传递到数据库的凭据。默认情况下,SQL Server 2005 和 SQL Server 2008 将对凭据进行加密。为了提高安全性,请将“Encrypt”连接属性设置为“on”,以便对发送至服务器的所有数据进行加密。注意: 将“Encrypt”连接属性设置为“on”可能导致性能降低,因为数据加密可能是一项计算密集型操作。在PHP 脚本的纯文本部分中不要包含 UID 和PWD 连接属性的值。这些值应存储在具有相应受限权限的特定应用程序目录中。避免使用 sa 帐户。将应用程序映射到拥有所需权限的数据库用户,并使用强密码。注意: 在建立连接时可以设置除 UID 和PWD 之外的连接属性。有关支持的连接属性的完整列表,请参阅 sqlsrv_connect。示例 下面的示例使用 SQL Server 身份验证连接到 SQL Server 的本地实例。所需的 UID 和PWD 连接属性的值是从 C:\AppData 目录中特定应用程序的文本文件 uid.txt 和pwd.txt 中提取的。建立连接之后,将查询服务器以验证用户登录名。 此示例假定本地计算机上已安装了 SQL Server 和 AdventureWorks 数据库。当从浏览器运行此示例时,所有的输出都将写入该浏览器。 <?php /* Specify the server and connection string attributes. */ $serverName = "(local)"; /* Get UID and PWD from application-specific files. */ $uid = file_get_contents("C:\AppData\uid.txt"); $pwd = file_get_contents("C:\AppData\pwd.txt"); $connectionInfo = array( "UID"=>$uid, "PWD"=>$pwd, "Database"=>"AdventureWorks"); /* Connect using SQL Server Authentication. */ $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn === false ) { echo "Unable to connect.</br>"; die( print_r( sqlsrv_errors(), true)); } /* Query SQL Server for the login of the user accessing the database. */ $tsql = "SELECT CONVERT(varchar(32), SUSER_SNAME())"; $stmt = sqlsrv_query( $conn, $tsql); if( $stmt === false ) { echo "Error in executing query.</br>"; die( print_r( sqlsrv_errors(), true)); } /* Retrieve and display the results of the query. */ $row = sqlsrv_fetch_array($stmt); echo "User login: ".$row[0]."</br>"; /* Free statement and connection resources. */ sqlsrv_free_stmt( $stmt); sqlsrv_close( $conn); ?
SQLServer身份验证只能在无法进行Windows身份验证时使用。有关使用Windows身份验证进行连接的信息,请参阅如何使用Windows身份验证进行连接。在使用SQLServer身份验证连接到SQLServer时必须考虑以下几点:必须对服务器启用SQLServer混合模式身份验证。在尝试建立连接时必须设置UID和PWD连接属性。UID和PWD必须映射到有效的SQLServer用户和密码。注意:包含右大括号(})的密码必须使用另一个右大括号进行转义。例如,如果SQLServer密码为“pass}word”,则PWD连接属性的值必须设置为“pass}}word”。在使用SQLServer身份验证连接到SQLServer时应采取以下预防措施:保护(加密)通过网络从Web服务器传递到数据库的凭据。默认情况下,SQLServer2005和SQLServer2008将对凭据进行加密。为了提高安全性,请将“Encrypt”连接属性设置为“on”,以便对发送至服务器的所有数据进行加密。注意:将“Encrypt”连接属性设置为“on”可能导致性能降低,因为数据加密可能是一项计算密集型操作。在PHP脚本的纯文本部分中不要包含UID和PWD连接属性的值。这些值应存储在具有相应受限权限的特定应用程序目录中。避免使用sa帐户。将应用程序映射到拥有所需权限的数据库用户,并使用强密码。注意:在建立连接时可以设置除UID和PWD之外的连接属性。有关支持的连接属性的完整列表,请参阅sqlsrv_connect。示例下面的示例使用SQLServer身份验证连接到SQLServer的本地实例。所需的UID和PWD连接属性的值是从C:\AppData目录中特定应用程序的文本文件uid.txt和pwd.txt中提取的。建立连接之后,将查询服务器以验证用户登录名。此示例假定本地计算机上已安装了SQLServer和AdventureWorks数据库。当从浏览器运行此示例时,所有的输出都将写入该浏览器。<?php/*Specifytheserverandconnectionstringattributes.*/$serverName="(local)";/*GetUIDandPWDfromapplication-specificfiles.*/$uid=file_get_contents("C:\AppData\uid.txt");$pwd=file_get_contents("C:\AppData\pwd.txt");$connectionInfo=array("UID"=>$uid,"PWD"=>$pwd,"Database"=>"AdventureWorks");/*ConnectusingSQLServerAuthentication.*/$conn=sqlsrv_connect($serverName,$connectionInfo);if($conn===false){echo"Unabletoconnect.</br>";die(print_r(sqlsrv_errors(),true));}/*QuerySQLServerfortheloginoftheuseraccessingthedatabase.*/$tsql="SELECTCONVERT(varchar(32),SUSER_SNAME())";$stmt=sqlsrv_query($conn,$tsql);if($stmt===false){echo"Errorinexecutingquery.</br>";die(print_r(sqlsrv_errors(),true));}/*Retrieveanddisplaytheresultsofthequery.*/$row=sqlsrv_fetch_array($stmt);echo"Userlogin:".$row[0]."</br>";/*Freestatementandconnectionresources.*/sqlsrv_free_stmt($stmt);sqlsrv_close($conn);?