Using active directory to authenticate users on intranet site

Using active directory to authenticate users on intranet site

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

Stefan Bogdanescu

Founder & Senior Architect

Integrating Active Directory Authentication into PHP and Apache Environments

Migrating an existing application from a custom database-backed user store to Active Directory (AD) authentication is a standard requirement for enterprise intranets. The goal is straightforward: when a user accesses http://intranetsite/mySite, their Windows domain credentials should be validated against the AD server, and upon success, they are granted access to the main page without entering separate site-specific passwords.

Since your application stack consists of PHP running on Apache while the directory service resides on an IIS server, you must rely on protocols that bridge this gap. Direct database integration with AD is generally discouraged for this specific use case because it couples your application tightly to a legacy directory schema and requires managing complex LDAP connection strings within your codebase. The industry-standard approach leverages Kerberos or NTLM challenge-response mechanisms, which are natively supported by Apache via the mod_auth_kerb or mod_authn_sspi modules.

Configuring Apache for AD Authentication

Before writing any PHP logic, you must configure the web server to handle the initial authentication handshake. Apache acts as the gateway; it intercepts the request and validates credentials against the domain controller before allowing the script to execute.

You need to ensure that the mod_auth_kerb module is loaded in your Apache configuration. This module handles Kerberos ticket validation, which is the preferred security protocol for modern Windows domains. If your environment relies on older NTLM protocols, you would look at mod_authn_sspi, but Kerberos is recommended unless legacy compatibility forces otherwise.

In your .htaccess file or main Apache virtual host configuration, you define the authentication requirements. For a directory-level protection, the configuration typically looks like this:

AuthType Kerberos
AuthName "Intranet Access"
AuthKrb5OnFail Off
Require valid-user

Placing this in .htaccess is convenient for development or isolated environments, but for production, it is often better to manage these directives directly in the <Directory> block of your main configuration file. This ensures that the authentication happens before any PHP code runs, preventing unauthorized access even if the script logic is bypassed.

Implementing PHP Logic for Session Management

Once Apache validates the credentials, the user is effectively authenticated at the server level. However, your PHP application still needs to recognize who the user is. You do not need to re-validate the password in PHP; instead, you must map the authenticated Windows identity to a session variable.

When mod_auth_kerb or mod_authn_sspi validates a ticket, it sets specific environment variables accessible via PHP. The most critical one is typically found in the $_SERVER superglobal. You can check for the presence of these variables to determine if the user passed the AD gate.

// Check if Kerberos authentication succeeded and retrieve identity
if (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['KRB5_CC_NAME'])) {
    $username = $_SERVER['PHP_AUTH_USER'] ?? $_ENV['USERNAME']; // Fallback logic varies by module
    
    // Store in session to persist across page requests
    if (!isset($_SESSION['user_identity'])) {
        $_SESSION['user_identity'] = $username;
        // Optionally, fetch user details from a local DB or cache using the username
    }
} else {
    // Redirect to login or show access denied if authentication fails
    header('Location: /login.php?error=auth_failed');
    exit;
}

It is crucial to note that you should not attempt to parse the raw Kerberos ticket or NTLM hash within PHP. The web server modules handle the cryptographic handshake and token validation. Your PHP code's responsibility is strictly session management: storing the validated username and ensuring it persists across redirects and form submissions.

Trade-offs and Configuration Considerations

A significant trade-off in this architecture is the dependency on specific environment variables. If you switch from Kerberos to NTLM, or if your Apache module version changes, the variable names in $_SERVER might differ. For instance, some configurations expose the Windows username as $_ENV['USERNAME'] rather than a direct $_SERVER entry. You must inspect your specific Apache logs and environment to determine exactly which keys are populated upon successful AD validation.

Another pitfall involves single sign-on (SSO) consistency. If a user has multiple monitors or tabs open, they may have different Kerberos tickets cached locally. Apache usually uses the most recent ticket presented in the HTTP request header (Authorization). This can sometimes lead to inconsistencies if the client's ticket cache is out of sync with the server's time, causing 401 Unauthorized errors even for valid users. Ensuring accurate time synchronization between your application server and the Domain Controller is mandatory; a drift of more than five minutes will cause authentication failures.

If you are using a framework like Laravel, consider leveraging packages that abstract these details, but be aware that they still require the underlying Apache modules to be configured correctly first. The link to LaravelCompany can provide further resources on integrating legacy Windows authentication with modern PHP frameworks if your specific setup requires additional abstraction layers.

Ultimately, this approach separates concerns cleanly: the web server handles the heavy lifting of validating domain credentials, while the application focuses on business logic and user session state.

Our network