Hướng dẫn php windows authentication

Chuyển đến nội dung chính

Trình duyệt này không còn được hỗ trợ nữa.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

How to: Connect Using Windows Authentication

  • Bài viết
  • 09/09/2022
  • 2 phút để đọc

Trong bài viết này

Hướng dẫn php windows authentication
Download PHP driver

By default, the Microsoft Drivers for PHP for SQL Server use Windows Authentication to connect to SQL Server. It is important to note that in most scenarios, this means that 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.

The following points must be considered when you use Windows Authentication to connect to SQL Server:

  • The credentials under which the Web server's process (or thread) is running must map to a valid SQL Server login in order to establish a connection.

  • If SQL Server and the Web server are on different computers, SQL Server must be configured to enable remote connections.

Note

Connection attributes such as Database and ConnectionPooling can be set when you establish a connection. For a complete list of supported connection attributes, see Connection Options.

Windows Authentication should be used to connect to SQL Server whenever possible for the following reasons:

  • No credentials are passed over the network during authentication; user names and passwords are not embedded in the database connection string. This means that malicious users or attackers cannot obtain the credentials by monitoring the network or by viewing connection strings inside configuration files.

  • Users are subject to centralized account management; security policies such as password expiration periods, minimum password lengths, and account lockout after multiple invalid logon requests are enforced.

If Windows Authentication is not a practical option, see How to: Connect Using SQL Server Authentication.

SQLSRV example

Using the SQLSRV driver of the Microsoft Drivers for PHP for SQL Server, the following example uses the Windows Authentication to connect to a local instance of SQL Server. After the connection has been established, the server is queried for the login of the user who is accessing the database.

The example assumes that SQL Server and the AdventureWorks database are installed on the local computer. All output is written to the browser when the example is run from the browser.

<?php  
/* Specify the server and connection string attributes. */  
$serverName = "(local)";  
$connectionInfo = array( "Database"=>"AdventureWorks");  
  
/* Connect using Windows 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);  
?>  

PDO_SQLSRV example

The following example uses the PDO_SQLSRV driver to accomplish the same task as the previous sample.

<?php  
try {  
   $conn = new PDO( "sqlsrv:Server=(local);Database=AdventureWorks", NULL, NULL);   
   $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
}  
  
catch( PDOException $e ) {  
   die( "Error connecting to SQL Server" );   
}  
  
echo "Connected to SQL Server\n";  
  
$query = 'select * from Person.ContactType';   
$stmt = $conn->query( $query );   
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){   
   print_r( $row );   
}  
?>  

See Also

How to: Connect Using SQL Server Authentication

Programming Guide for the Microsoft Drivers for PHP for SQL Server

About Code Examples in the Documentation

How to: Create a SQL Server Login

How to: Create a Database User

Managing Users, Roles, and Logins

User-Schema Separation

Grant Object Permissions (Transact-SQL)

Phản hồi

Gửi và xem ý kiến phản hồi dành cho

Hướng dẫn php windows authentication

Hướng dẫn php windows authentication

Câu hỏi phỏng vấn oop php

Mở đầu.Xin chào anh em. Hôm nay rảnh rỗi hồi tưởng quá khứ, tự nhận thấy bản thân cũng đã trải qua khá nhiều lần phỏng vấn nên hôm nay mình có tổng ...

Hướng dẫn php windows authentication

Hướng dẫn dùng closuer trong PHP

Trong quá trình làm quen với các framework PHP như Laravel, Symfony... chúng ta rất hay gặp các khái niệm Lambda, Closure, chúng đều là những tính năng mới cho phép tái ...

Hướng dẫn php windows authentication

Basic graphics concepts in php

PHP can be used to create and manipulate graphical elements independently. Therefore, the scope and capabilities of PHP graphic handling are very powerful.It does this with the aid of the GD library, ...

Hướng dẫn php windows authentication

Hướng dẫn dùng call anonymous trong PHP

Callback()??? Thứ mà các bạn vẫn nghe ra rả (hoặc đọc) trong các tutorial về Javascript thì giờ đây còn xuất hiện trong cả PHP. À thật ra thì nó có từ thời ...

Hướng dẫn php windows authentication

Can we use php variable in jquery?

I ran into a similar issue when building a custom pagination for a site I am working on.The global variable I created in functions.php was defined and set to 0. I could output this value in my ...

Hướng dẫn php windows authentication

How to add calendar in php form?

PHP Calendar IntroductionThe calendar extension contains functions that simplifies converting between different calendar formats.It is based on the Julian Day Count, which is a count of days starting ...

Hướng dẫn php windows authentication

Php pass anonymous function as parameter

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. ...

Hướng dẫn php windows authentication

Hướng dẫn dùng class const trong PHP

Ta có thể định nghĩa hằng giá trị trong một lớp. Hằng được sử dụng như những biến thông thường của lớp, nhưng không sử dụng ký hiệu $ để tạo.Giá ...

Hướng dẫn php windows authentication

Tìm thứ trong tuần php

Viet Matrix Revolutions nay tiếp tục bài PHP cơ bảnYêu cầu : Viết 1 ứng dụng khi nhập ngày – tháng – năm nhấn nút button thì ứng dụng xuất ra ngày – tháng – ...

Hướng dẫn php windows authentication

Hướng dẫn dùng 64encode trong PHP

Chuyển đến nội dungCó nhiều giải pháp để mã hoá hình ảnh, Encode Base64 là một trong số đó. Khi chuyển hình ảnh về dạng này ảnh được hiển thị trực ...

Hướng dẫn php windows authentication

Php read file names from directory

I want to read html file name from directory without any database. I have a code and its working properly, but two blank name it is giving, while I have only 4 file in directory. <?php if ...

Hướng dẫn php windows authentication

Hướng dẫn dùng install ext-bcmath trong PHP

Skip to contentHOMESERVICES PHP DevelopmentLaravel DevelopmentiPhone App DevelopmentAndroid App DevelopmentWooCommerce DevelopmentMagento DevelopmentShopify DevelopmentWordPress DevelopmentWordPress ...

Hướng dẫn php windows authentication

Final class in php with example

Definition of PHP Final ClassPHP final class is a class which prevents overriding a method of the child classes just by the final prefix with the definition. It means that if we are defining a method ...

Hướng dẫn php windows authentication

Hướng dẫn dùng hege library trong PHP

Tôi biết người đàn ông đấu tranh! Nhưng gần đây tôi đã làm việc khá nhiều với Workerman. Nếu bạn chưa vấp phải framework php này thì tốt hơn hết bạn nên ...

Hướng dẫn php windows authentication

Hướng dẫn preg_match and replace php

1. Biểu thức chính qui (Regular Expression) là gì? Biểu thức chính quy (Regular Expression) là thuật toán khớp mẫu mạnh mẽ có thể được thực hiện trong một biểu ...

Hướng dẫn php windows authentication

Hướng dẫn dùng roll bac trong PHP

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)PDO::rollBack — Rolls back a transaction Descriptionpublic PDO::rollBack(): bool If the database was set to autocommit mode, this function ...

Hướng dẫn php windows authentication

How to minus two date in php

i want to minus two dates in phpfor example:$date1 = 08/16/2013; $date2 = 08/23/2013; $answer = date2 - date1; the $answer should be 7, How will i do that? thank you so much asked Aug 23, 2013 at ...

Hướng dẫn php windows authentication

Hướng dẫn dùng post method trong PHP

Khi các bạn truy cập sử dụng website thì bạn chính là một Client. Khi các bạn đăng nhập hay đăng ký tài khoản, hoặc là các bạn đăng comment thì dữ liệu sẽ ...

Hướng dẫn php windows authentication

Hướng dẫn update php wordpress ubuntu

WordPress is one of the most popular free web publishing tools available. Authoring content on WordPress is straightforward, however, you need to perform regular maintenance on your WordPress site to ...

Hướng dẫn php windows authentication

Php datetime add 1 month

I agree with the sentiment of the OP that this is counter-intuitive and frustrating, but so is determining what +1 month means in the scenarios where this occurs. Consider these examples:You start ...

Hướng dẫn php windows authentication

Hướng dẫn reset post data php

In order to prevent a form from being re-submitted, you could do something like this - it should be pretty self explanatory:function uniquePost($posted) { // take some form values ...

Hướng dẫn php windows authentication

Hướng dẫn php windows authentication

Hướng dẫn dùng uoad trong PHP

Thủ Thuật Hướng dẫn Hướng dẫn dùng uplooad trong PHP Chi TiếtBạn đang tìm kiếm từ khóa Hướng dẫn dùng uplooad trong PHP được Cập Nhật vào lúc : 2022-09-14 ...

Hướng dẫn php windows authentication

How to execute php program steps

XAMPP is a Web development tool, created by Apache, that makes it easy to run PHP (Personal Home Pages) scripts on your computer locally. Installation of XAMPP Server on windows is easy as compared ...

Hướng dẫn php windows authentication

Hướng dẫn php link to html

Hàm chuyển hướng url trong php giúp chương trình chúng ta chủ động chuyển đến một địa chỉ url được chỉ định một cách dễ dàng.Cú phápheader(location: ...

Hướng dẫn php windows authentication

Hướng dẫn delete function in php

Trong bài này, chúng ta sẽ tìm hiểu về cách delete row trong table với PHP. Để học tốt bài này, các bạn cần đọc lại bài Cài đặt môi trường lập trình Web ...

Hướng dẫn php windows authentication

Hướng dẫn php windows authentication

Install php from tar gz

Rar is currently available through PECL » https://pecl.php.net/package/rar. Also you can use the PECL installer to install the Rar extension, using the following command: pecl -v install rar. ...

Hướng dẫn php windows authentication

How do i upgrade php on windows?

Im trying to get my Laravel project to work. But when I use composer update it says the following:This package requires php >=5.6.4 but your PHP version (5.5.12) does not satisfy that ...

Hướng dẫn php windows authentication

Hướng dẫn dùng url sign trong PHP

Chương trình họcTác giả: Dương Nguyễn Phú CườngNội dung chínhChương trình họcNhu cầu mã hóa và giải mã URLMã hóa URL với urlencode() và rawurlencode() Giải mã ...

Hướng dẫn php windows authentication

Read text file to array php

I am trying to read every line of a text file into an array and have each line in a new element. My code so far.<?php $file = fopen(members.txt, r); while (!feof($file)) { $line_of_text = ...

Hướng dẫn php windows authentication

Hướng dẫn dùng .loop trong PHP

Trong bài học này, bạn sẽ học cách sử dụng vòng lặp while, do…while, for, foreach để lặp lại một loạt các hành động trong PHP.Các vòng lặp được sử dụng ...

Hướng dẫn php windows authentication

Hướng dẫn php array sum column

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)array_sum — Calculate the sum of values in an arrayDescriptionarray_sum(array $array): int|floatParameters array The input array. Return Values Returns the ...

Hướng dẫn php windows authentication

Hướng dẫn dùng endswith trong PHP

answer 1421Bùi Hà · Bùi Hà 12:24 07/05/2009function startsWith($haystack, $needle) { $length = strlen($needle); return (substr($haystack, 0, $length) === $needle); } function ...

Hướng dẫn php windows authentication

Php form data to mongodb

To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the driver from the url Download PHP Driver. Make sure to download the latest release of it. Now unzip the archive and put ...

Hướng dẫn php windows authentication

Hướng dẫn dùng twig replace trong PHP

str_replace trong TwigTôi muốn tạo một str_replace đơn giản trong mẫu cành cây của mình. Tôi mới sử dụng twig và có lẽ tôi cần thêm bộ lọc mới hoặc sth ...

Hướng dẫn php windows authentication

Hướng dẫn php windows authentication

Get current time in minutes php

The PHP date() function is used to format a date and/or a time.The PHP Date() FunctionThe PHP date() function formats a timestamp to a more readable date and ...

Hướng dẫn php windows authentication