When using SQL Server, Windows Authentication is successful when connecting through Management Studio but not when using Apache

My PHP application built with Codeigniter is attempting to connect to SQL Server 2016 using Windows Authentication.

Encountering the following error message:

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 'AAA\abcd'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'AAA\abcd'. ) )

The same Windows account credentials work flawlessly when used in the SQL Server Management tool.

Additionally, connecting to SQL Server using a SQL server user account works without any issues.

What could I be overlooking?

Answer №1

For those looking to connect to SQL Server using Windows authentication, simply leave the username and password fields empty. If you do provide a username and password, SQL authentication will be used instead.

When utilizing Windows authentication, the Web server's process identity is utilized for connecting to the server. This explains why you are able to access the server with SQL Server Management Studio (as it authenticates with your Windows credentials), but encounter script failures.

If you have PHP Driver for SQL Server installed, consider running this script to check under which account your script is being executed:

<?php
$server = 'server\instance,port';
$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);
?>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining SELECT queries for merging similar results in a non-DISTINCT result set

---------- ---------- ---------- ---------- | t1 | | t2 | | t3 | | t4 | ---------- ---------- ---------- ---------- | id | | id | | id | | t1_id | | foo | | bar | | foobar | | t2_id ...

How to use PHP code to decode Instagram URLs and return Null values

Can anyone help with an issue regarding json_decode from Instagram URL? When using the following URL , it returns Null in PHP. $newwul = "http://www.instagram.com/username/?__a=1"; $xo = connect($newwul); $xx = json_decode($xo,tru ...

What are the names of these limitations? Is there a way for me to learn more about them?

Hey there, I've been experimenting with setting up a 'custom' ACL that includes additional constraints. Typically, an ACL check looks like this: if(aclCheck($user, 'edit', 'really_important_value')){ // Allow $user t ...

Automated PHP Link Generation on a Monthly Basis

I've been stuck in this endless loop for hours now and I'm too exhausted to figure out the issue. The desired result should be: <li><a href="monthly/13-7.php">July 2013</a></li><li><a href="monthly/13-8.php">Au ...

Transmitting unique characters, such as a caron symbol, via xmlhttp.responseText and encoding them with json_encode

I am struggling to retrieve data from a database that contains a special character (caron) and then pass it through xmlhttp.responseText using json_encode to fill textboxes. However, the textbox linked to the data with the special character (caron) is not ...

Updating the Wordpress menu: Get rid of the list items and instead add the class ".current-menu-item" directly to the anchor tag

Is there a way to customize the output of wp_nav_menu(); to display a navigation menu like this: <nav> <a>Menu Item</a> <a class="current-menu-item">Menu Item</a> <a>Menu Item</a> <a>Menu Ite ...

In PHP, implement a function to insert a delimiter into a string retrieved from a

Merchant Id|Merchant|Website|Transaction In Period|Voids In Period|Gross Sales Amount|Total Commision in Period 9766|Mountains Plus Outdoor Gear|www.MPGear.com|1|0|88.91|8.89 12447|Meritline.com|www.meritline.com|5|0|52.86|3.71 16213|East Coas ...

Collect information from a mySQL database and store it in an array

My data table, CallTriggers, has the following attributes: cloturer = 0 or 1 created_at I am looking to retrieve all the data in the following format: $array = [ ['Month', 'Total Call', 'Close'], [&ap ...

Integrating blade code within blade code

Struggling to craft a button using the Form builder. {!! Form::button('<span style="color: {!! $colour[$i] !!}" class..') !!} The specific details of the button don't matter, all I wanted was to adjust the font color to $colour[$id]. Th ...

The ImageMagick 6.7.9 encountered an error when attempting to write a blob

Looking to generate an image from a PDF file? I've already got ImageMagick set up on my Linux server and managed to convert a PDF into an image using the root access through the terminal. Here's the command I used: /usr/local/bin/convert http:/ ...

Creating a PHP script that retrieves data from JavaScript and stores it in MySQL can be accomplished by using AJAX to send the

Hello, I am attempting to create a PHP script that can extract coordinates from this JavaScript code (or from this link ) and store them in a MySQL database. Can someone please provide me with a tutorial on how to accomplish this? <script> var ...

Implementing dynamic database content into a Wow Slider on a jQuery Mobile platform

I am currently using static data in a slider with wow slider jquery mobile. However, I would like to retrieve data from a database and display it in the slider. Can someone provide suggestions on how to do this? Thank you. <script type="text/javascri ...

I must adjust the size of images based on the size of the viewer's screen

Hello everyone, I could really use some assistance as I am not an expert programmer but just a site admin. My issue is this: I have a website running on PHP and I want to display images to my members while keeping them within specific size limits so they ...

What could be causing this prepared statement to consistently display -1 rows affected?

I'm really struggling with this mysqli prepared statement and I just can't seem to figure out what's going wrong. I've spent a lot of time troubleshooting already, so let me start by showing you the code snippet (which is currently only ...

Saving and accessing numbers in MySQL database

I'm encountering an issue with integers in MySQL. I'm attempting to update a cell that stores an integer value, but the type of the cell seems to be causing problems. Despite being set as int, whenever I retrieve the data, it always shows up as 0 ...

Display the same data point on a Google Map from two different rows of a database

I'm looking to display multiple values for profFName and profLName on a map with just one point showing this information. If I have (2) pID connected to the same marker, I want it to show both profFName and profLName on a single point. Currently, it ...

Populating hidden fields in Javascript with mouseover event trigger - Pre-click action

Seeking a solution for another issue, I stumbled upon the capability to execute JavaScript on mouseover. Here is what I want to execute: $(document).ready(function(){ function myPayment() { var value = document.mortgagecalc.value_input.value; var rate ...

Creating a multilingual website using PHP, MySQL, and cookies

Having developed a multilingual website using PHP and MySQL, I am currently facing issues related to robots, SEO, and search engines. This is mainly due to the fact that the language selection mechanism relies on cookies. The current process is as follows ...

How can we validate the radio buttons to show a value even if none of the buttons are checked?

In my registration form, I have a gender radio option with "male" or "female" choices. Both options are unchecked initially so the user must select one. Here is the HTML code: <label class="radio"> <input type="radio" name="gender" id="option ...

JavaScript is submitting an incorrect HTML form

I have a PHP script that retrieves items from a database and allows users to vote on them. Each item is associated with an HTML form containing an input text field and a submit button to enter the score. I also have a jQuery script on the same page that up ...