Php Ldap Login Page Example

PHP Programming

Today, I want to share with you an example of a PHP LDAP login page. As a web developer, I often find myself working on projects that require user authentication. One common approach is to use the Lightweight Directory Access Protocol (LDAP) to authenticate users against an LDAP directory. LDAP is commonly used in enterprise environments to store and manage user identities.

Before diving into the example, let’s quickly understand what LDAP is and how it works. LDAP is a protocol that allows you to access and manage directory information over a network. It provides a hierarchical structure to store and organize data, making it ideal for storing user records.

Now, let’s start building our PHP LDAP login page. First, we need to establish a connection to the LDAP server. We can use the ldap_connect() function to achieve this:

$ldap = ldap_connect("");

Next, we need to bind to the LDAP server using a valid username and password. This step is crucial as it verifies the user’s credentials. We can use the ldap_bind() function:

$ldapBind = ldap_bind($ldap, "cn=username,ou=users,dc=example,dc=com", "password");

Once the user is successfully bound to the LDAP server, we can proceed with the login process. In this example, let’s assume we have a simple HTML form with fields for username and password:

<form method="post" action="login.php">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="Login">

In the login.php file, we can retrieve the username and password entered by the user:

$username = $_POST['username'];
$password = $_POST['password'];

Now, let’s verify the user’s login credentials by binding to the LDAP server using the provided username and password:

$ldapBind = ldap_bind($ldap, "cn=$username,ou=users,dc=example,dc=com", $password);

If the binding is successful, the user’s credentials are valid. We can then redirect them to the desired page:

header("Location: welcome.php");

On the other hand, if the binding fails, we can display an error message to the user:

echo "Invalid username or password";

Don’t forget to close the LDAP connection once you’re done:


That’s it! We’ve successfully created a PHP LDAP login page. This example demonstrates the basic process of authenticating users against an LDAP server. However, keep in mind that real-world implementations might require additional steps, such as handling SSL/TLS encryption and validating user input.


In this article, we explored the concept of PHP LDAP login pages and how they can be used to authenticate users against an LDAP server. We learned how to establish a connection to the LDAP server, bind to it with a valid username and password, and verify user credentials. Remember to always handle user input securely, apply proper error handling, and close the LDAP connection when finished.

If you want to try this example yourself or learn more about LDAP authentication, you can find the complete code and resources on the GitHub repository dedicated to this topic.