Home » Active Directory Using LDAP In PHP Or Laravel

Active Directory Using LDAP In PHP Or Laravel

Last updated on December 27, 2020 by

In this article, we will learn Active Directory implementation using LDAP in PHP or Laravel. LDAP is the Lightweight Directory Access Protocol, and is a protocol used to access “Directory Servers”. The Directory is a special kind of database that holds information in a tree structure. Let’s see the overview and then implement it with an example.

What Is Active Directory In Simpler Word?

In a very simpler word, Active Directory (AD) is a directory service developed by Microsoft for Windows domain networks. It is used to authenticate and authorize users and computers, it allows management and storage of information in a network.

Let’s understand the Active Directory, for example, you have 100 offices and all the 100 offices have a large number of employees. So to manage each & every employee you have the systems in each offices to track different records.

The employee can log in to the system and can see the different activities they have done and they will do in the future. So instead of managing all the records from individual offices, you will create a central(main) system from where every data can be managed structurally and shared among all the offices. That central system’s records are called Active Directory.

Why Active Directory(AD) Or LDAP Required?

There are lots of reasons to use Active Directory. It can vary from person to person but some of them are below.

  1. The client always demanding performance in large-scale projects so that AD is used for performance enhancement.
  2. Suppose, Clients have thousands of websites and they have lots of registered users. So that by using AD or LDAP they can store all the user information in a single place. So just by a single query, they can easily authenticate.
  3. A third and most important reason for using AD or LDAP. For security reasons, they can use AD or LDAP. This is the most secure way to protect user’s information in a single place. LDAP can work with SSL & TLS and thus can be used for.
  4. LDAP’s main usage is to provide faster retrieval of the data. It acts as a central repository for storing user details that can be accessed by various applications at the same time.
  5. LDAP apart from supporting the data recovery capability. Also, allows us to export data into an LDIF file that can be read by various software available in the market.
  6. Active Directory helps you to organize your company’s users, computers, and more. Your IT administrator uses the AD to organize your company’s complete hierarchy from which computers belong to which network, to what your profile picture looks like or which users have access to the storage room.

LDAP Terminology

LDAP Terminology is the most important thing that needs to understand before LDAP implementation.

  • DN: DN stands for the Distinguish Name. DN is used to uniquely identify entry into the Active Directory. For example, uid=john.doe
  • DC: DC stands for the Domain Component. As the name suggests, it is used to identify the Domain Name. For example, www.scratchcode.io would be written as DC=www, DC=scratchcode, DC=io
  • OU: OU stands for the Organizational Unit. OU is also called user group. I can also call like the user is part of it like Any University, Any Degree, Any City, etc. For example, OU=USA, OU=Doctor, OU=California.
  • CN: CN stands for the Common Name. If you want to query for the individual object then this will be used like Persons Name, Persons Mobile Number, etc.

Free LDAP Test Server For Testing

If you have a question in your mind that is there any free LDAP test server? so that you can easily test LDAP functionality. There are lots of websites providing free LDAP service for only testing. This is only for testing while your clients providing you with all the details for LDAP or AD. Check free LDAP test server

LDAP Server Information (read-only access):

Server: ldap.forumsys.comPort: 389
Bind DN: cn=read-only-admin,dc=example,dc=com
Bind Password: passwordou=mathematicians,dc=example,dc=comou=scientists,dc=example,dc=com

You can use followings uid for the uniquely identified user.

  • riemann
  • gauss
  • euler
  • euclid
  • einstein
  • newton
  • galieleo
  • tesl

Example Of Active Directory Implementation Using LDAP in PHP or Laravel

<?php

	$ldap_con = ldap_connect("ldap.forumsys.com",389);
	$ldap_dn = "cn=read-only-admin,dc=example,dc=com";
	$ldap_ps = "password";

	ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);

	if (ldap_bind($ldap_con, $ldap_dn, $ldap_ps)):

		echo "Ldap binding successful";

		/*@ Getting data START */  
		
		$filter = ("uid=newton"); 
		
		$results = ldap_search($ldap_con, "dc=example,dc=com", $filter);  
		
		$search_result = ldap_get_entries($ldap_con, $results);  

		/*@ Getting data ENDS */    

 	else:

  		echo "Ldap binding not successful";
 	endif;

Notes: You need to enable LDAP extension in php.ini file so that it will start working otherwise it will throw an error.

Notes: I recommended you to test this script on the live server instead of local because there are lots of issues on the local server.

Get more information about the LDAP functions from here.

Additionally, read our guide:

  1. Best Way to Remove Public from URL in Laravel
  2. Error After php artisan config:cache In Laravel
  3. Specified Key Was Too Long Error In Laravel
  4. AJAX PHP Post Request With Example
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Calculate Age From Birthdate
  11. How to Convert Base64 to Image in PHP
  12. Check If A String Contains A Specific Word In PHP
  13. How To Find Duplicate Records in Database

That’s it for now. We hope this article helped you to learn Active Directory Implementation Using LDAP in PHP or Laravel

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance. 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment