Can you get a Windows (AD) username in PHP?

Can you get a Windows (AD) username in PHP?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Can you get a Windows (AD) username in PHP?

Extracting the local IP address or hostname from a web request is standard practice for server logging and basic access control. However, attempting to retrieve an Active Directory (AD) username directly from a PHP script running on a web server usually hits a hard wall: the web service process has no visibility into the logged-in user's credentials. The HTTP protocol itself does not transmit authentication tokens that would map back to a specific Windows account in the browser or the client machine.

To understand why this is difficult, you have to look at how browsers and servers interact. When a user logs into their Windows machine, that session exists locally on their endpoint. Unless they explicitly send that information to your web server, it remains isolated. Standard headers like REMOTE_ADDR provide the IP address, but there is no equivalent header for "Logged-In-User" because that data is never part of the HTTP request stream by default.

If you need this information, you must establish a trust relationship between your web application and your directory service. This typically involves implementing LDAP (Lightweight Directory Access Protocol) or Kerberos authentication. The approach depends entirely on whether your users are accessing the site via HTTPS with SSO enabled or if they are simply browsing an internal app without logging into the browser itself.

Implementing LDAP Authentication

The most direct method to get a Windows username is to force an LDAP bind against your domain controller. This requires your PHP server to have network access to the AD domain and knowledge of the directory structure. You will likely need the php-ldap extension installed.

In this model, you present a login form where users enter their domain credentials (e.g., DOMAIN\jdoe). The application then attempts to authenticate these credentials against the LDAP server. If successful, the server receives the authenticated user object, which contains the sAMAccountName or userPrincipalName.

Here is a conceptual example using PHP's LDAP extension:

<?php
$ldap_server = "dc=company,dc=com";
$user_input = $_POST['username']; // e.g., "jdoe"
$pass_input = $_POST['password']; // e.g., "Secret123!"

// Construct the DN based on your directory structure
$dn = "cn=$user_input,ou=Users,dc=company,dc=com";

$conn = ldap_connect($ldap_server);
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3); // Use LDAPv3 for Kerberos support if needed

// Bind with the provided credentials
$result = @ldap_bind($conn, $dn, $pass_input);

if ($result) {
    // Success: Retrieve specific attributes to ensure we have the correct user
    $search_result = ldap_search($conn, $dn, "(objectClass=person)", "userPrincipalName");
    if ($entry = ldap_first_entry($conn, $search_result)) {
        $username = ldap_get_values($conn, $entry, 'sAMAccountName');
        echo "Authenticated User: " . $username[0];
    }
} else {
    echo "Authentication failed.";
}

This approach requires users to input their credentials into the web form. The security implication here is significant. If you store passwords or transmit them without encryption, you expose them to interception. In an intranet environment, this might be acceptable, but it generally violates modern least-privilege principles.

Leveraging Kerberos and Single Sign-On (SSO)

For a more seamless experience where the username is available automatically without user input, you must rely on Kerberos SSO. This requires configuring your web server to accept Windows Integrated Authentication.

With Apache, this usually involves enabling mod_auth_negotiate or mod_auth_kerb. With Nginx, you would use ngx_http_perl_module or specific modules to handle the SPNEGO handshake. The PHP script then checks for specific headers injected by the browser during the authentication handshake.

Commonly, the header will be PHP_AUTH_USER if using basic auth mechanisms, but for Kerberos, the server validates the token and often sets the user context internally. In PHP, you can check for the existence of the authenticated user via:

$username = $_SERVER['REMOTE_USER'] ?? null;
if ($username) {
    echo "Current AD User: $username";
}

However, this only works if the web server has been explicitly configured to forward the authenticated identity from the browser session to the PHP process. Without this configuration, $_SERVER['REMOTE_USER'] will be null.

The Intranet Context and Security Trade-offs

Since your application is running on an intranet, you likely have control over both the client environment and the server infrastructure. This allows for the deployment of Group Policy Objects (GPO) to enforce SSO or SSL offloading that makes Kerberos transparent to the user.

The primary pitfall in this scenario is mixing authentication methods. If you switch from a simple form-based login to SSO, existing sessions break unless handled correctly. Additionally, relying on LDAP binds for every request adds latency and load to your domain controllers. A better architecture often involves an initial bind to validate the session once, then caching that identity for subsequent requests within the same session lifetime.

Finally, ensure that your PHP environment is isolated. If you are extracting user data, do not log sensitive AD attributes like distinguishedName or clear-text passwords in error logs. Stick to identifying usernames (sAMAccountName) and avoid exposing directory structure details that could aid lateral movement attacks within your network.

Our network