PHP LDAP LOGIN

PHP on Linux authenticate users with a Windows Server Active Directory

A common request when building INTRANET web applications is to have users use only one common shared set of login credentials. Typically in most small and medium businesses this means that logging into a Windows Server of some kind to access the network. Windows Servers use Active Directory (AD) ,which is basically Microsoft’s glorified LDAP server with a slew of additional features needed for coroporate and enterprise users. We can leverage this by using PHP’s LDAP module to perform the login check for us..

IF you’re building your application in PHP a very easy way to do this is simply to use PHPs LDAP library and then simply call with the proper functions. Let’s detail the steps below. I’ll be doing this from a PHP 5 , Debian installation.

LDAP tends to be tied into the Windows AD Domain Name System to allow integrated quick lookups and fast resolution of queries. LDAP generally runs on port 389 and like other protocols tends to usually conform to a distinct set of rules (RFC’s).

Step 1. Get the LDAP information (LDAP Connection URL) from your Windows Active Directory.

First we need to verify which Windows Active directory we will be authenticating against. If your in a very large corporate environment, you may need the help of an SA (System administrator) to help guide you. Basically, the first thing you need is to know is the Domain which this users is associated with.

Essetially you need to figure out with the LDAP Connection URL is , and this requires a bit more information about how the Windows Network is configured, Typically its something like ,

CN=Users,DC=corp,DC=domain,DC=local

then you wold combine this with the URL of the Windows AD server

LDAP://dc1.corp.domain.com/ or LDAP://10.0.1.3/

To query for a particular user. If you have accesss to the Windows Server form the command line run.

C:\Users\Administrator> dsquery user -name To* "CN=Tony Jones,CN=Users,DC=Domain,DC=local" "CN=Tony Smith,CN=Users,DC=Domain,DC=local"

Alternatively from the Windows server desktop try

Active directory server -> Choose the Organization Unit OU -> Right Click -> Properties -> AttributeEditor -> DistinguishedName

The above information is necessary so you can fill in the details of the PHP script below.

Step 2. Verify PHP LDAP Library installed and enabled.

You can do this by running a simple viewing the PHP info to see which loaded modules are currently available on your server.

Читайте также:  Padding в браузерах css

Create a simple PHP file on your web server , call it test.php or something like that. When you run it it will display all the information that PHP /Apache has configured.

Run that file from your browser http://localhost/test.php and search for an entry like LDAP (Typically midway down the page between JSON and LIBXML ), if you see something like the screen below.

PHP info showing LDAP module is enabled.

If you do not see this enabled, then you may want to have it added to your PHP/Apache server . The simplest way if you have a typical Debian Linux access to your server’s shell is to run the command. For other flavors of Linux like Redhat/Centos a similar yum command should be available.

sudo apt-get install php5-ldap
sudo apt-get install php-ldap

The command should install the necessary library and restart the server, once again re-test that it’s enabled by running the above test.php and confirming the LDAP module is correctly loaded.

Step 3. Test PHP Login against Windows AD

Finally Upload this test LDAP script to your server and save it as ldap.php (or similar name) which does the follow…

  • Usig the supplied LDAP information , don’t forget to REPLACE the DOMAIN_FQDN and LDAP_SERVER define with your actual credentials
  • Takes the user supplied username and password and
  • Issues an LDAP bind to see if that’s a valid user
  • You get back either a True or False from the AD and you can proceed from there.
 elseif ($bind) < //determine the LDAP Path from Active Directory details $base_dn = array("CN=Users,DC=". join(',DC=', explode('.', DOMAIN_FQDN)), "OU=Users,OU=People,DC=". join(',DC=', explode('.', DOMAIN_FQDN))); $result = ldap_search(array($conn,$conn), $base_dn, "(cn=*)"); if (!count($result)) $err = 'Result: '. ldap_error($conn); else < echo "Success"; /* Do your post login code here */ >> > // session OK, redirect to home page if (isset($_SESSION['redir'])) < header('Location: /'); exit(); >elseif (!isset($err)) $err = 'Result: '. ldap_error($conn); ldap_close($conn); > ?>     

Login

'; ?> Password:
'. $err .'
Login:

Security Considerations:

By default, LDAP traffic is transmitted unsecured; this may or may not be a concern in an Intranet setting, for maximum safety use SSL technology to encrypt the traffic. Also most Windows Server AD will LOCK the account after n unsuccessful re-tries so be aware of this when testing your PHP login script.

Hopefully this will provide an easy way to integrate your PHP scripts with your INTRANET servers, making it easier to keep manage user accounts and access from one spot.

11 thoughts on “ PHP on Linux authenticate users with a Windows Server Active Directory ”

Thanks, this works far better than any other process I’ve seen and takes far less configuration. Obviously we need to set up sessions with this but this is a great starting point. I should note on line 80 you are missing a closing bracket.

Is any option to check if user can login or what kind of user we have – without getting username and password? By using some “server variable”, “server user id”, “server user login”, “server user fingerprint”? Our goal is that when user is opening website on localhost we know that this in “Mark Twain” ex. and we have nice screen for him – without username and password.

I think you should be able to query the ACTIVE Directory server as to USER ATTRIBUTES, this does require that the folks that created the account properly assigned those attributes. Take a look here at some attributes available on active driectory http://www.kouti.com/tables/userattributes.htm . Once you have gotten the attribute for a user then you can decide what to show. I know most companies don’t add any extra attributes, besides maybe the department or description , maybe you can key off those two.?

Similar to the commend about a missing bracket on line 80…The code is missing a closing angle bracket for the tr tag (>) on line 85. Maybe it was originally line 80 and is now line 85.

Источник

Установка поддержки LDAP в PHP на сервере LAMP Ubuntu

Чтобы включить поддержку LDAP на существующем веб-сервере Ubuntu Apache, вам необходимо установить пакет php5-ldap.

# sudo apt-get install php5-ldap

Это должно быть так, но в моей конкретной ситуации после запуска sudo apt-get install php5-ldap я получил следующую ошибку:

Reading package lists. Done Building dependency tree Reading state information. Done The following NEW packages will be installed php5-ldap 0 upgraded, 1 newly installed, 0 to remove and 12 not upgraded. Need to get 19.6 kB of archives. After this operation, 115 kB of additional disk space will be used. Err http://gb.archive.ubuntu.com/ubuntu/ natty-updates/main php5-ldap amd64 5.3.5-1ubuntu7.2 404 Not Found Err http://security.ubuntu.com/ubuntu/ natty-security/main php5-ldap amd64 5.3.5-1ubuntu7.2 404 Not Found [IP: 91.189.92.166 80] Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/p/php5/php5-ldap_5.3.5-1ubuntu7.2_amd64.deb 404 Not Found [IP: 91.189.92.166 80] E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

Чтобы решить эту проблему, мне пришлось запустить:

(это обновляет ваш локальный индекс пакета с последними изменениями, внесенными в репозитории).
и повторите приведенные выше команды.

Когда ldap установлен и включен, phpinfo () отобразит раздел ldap:

Пример использования функций PHP LDAP для поиска в Windows Active Directory.

Источник

Installing LDAP support in PHP on Ubuntu server

LDAP support in PHP is not enabled by default. To enable LDAP support on an existing Ubuntu Apache web server you need to install php5-ldap package.

Install php5-lda:
sudo apt-get install php5-ldap
Reboot apache
/etc/init.d/apache2 restart

Be sure to enable LDAP support within PHP. Make sure that you have installed the necessary packages for your distro.

  • RedHat EL based distro (CentOS 4.3): yum install php-ldap
  • Make sure that /etc/php.d/ldap.ini contains extension=ldap.so
  • Ubuntu 6.06.1 (Dapper Drake) and others: sudo apt-get install php-ldap or possibly: sudo apt-get install php5-ldap
  • Other distros (/etc/php5/apache2): Modify php.ini, and uncomment the line: ;extension=php_ldap.so change to: extension=php_ldap.so
  • Windows: Modify php.ini, and uncomment the line: ;extension=php_ldap.dll change to: extension=php_ldap.dll

Share this:

Like this:

Posted on March 12, 2014 March 12, 2014 by Mamet Rockafella This entry was posted in Linux . Bookmark the permalink.

One thought on “ Installing LDAP support in PHP on Ubuntu server ”

I got this message :
he following packages have unmet dependencies:
php5-ldap : Depends: php5-common (= 5.5.9+dfsg-1ubuntu4) but 5.5.9+dfsg-1ubuntu4.11 is to be installed
E: Unable to correct problems, you have held broken packages.

Источник

Php ldap install apt get install

LDAP support in PHP is not enabled by default. To enable LDAP support on an existing Ubuntu Apache web server you need to install php5-ldap package.

Install php5-lda:
sudo apt-get install php5-ldap
Reboot apache
/etc/init.d/apache2 restart

That should be it, but in my particular situation after running sudo apt-get install php5-ldap I got following error:

Reading package lists. Done
Building dependency tree
Reading state information. Done
The following NEW packages will be installed
php5-ldap
0 upgraded, 1 newly installed, 0 to remove and 12 not upgraded.
Need to get 19.6 kB of archives.
After this operation, 115 kB of additional disk space will be used.
Err http://gb.archive.ubuntu.com/ubuntu/ natty-updates/main php5-ldap amd64 5.3.5-1ubuntu7.2
404 Not Found
Err http://security.ubuntu.com/ubuntu/ natty-security/main php5-ldap amd64 5.3.5-1ubuntu7.2
404 Not Found [IP: 91.189.92.166 80]Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/p/php5/php5-ldap_5.3.5-1ubuntu7.2_amd64.deb 404 Not Found [IP: 91.189.92.166 80]E: Unable to fetch some archives, maybe run apt-get update or try with —fix-missing?

To resolve this I had to run:
apt-get update ( this updates your local package index with the latest changes made in repositories. )
and repeat the commands above.

LDAP support in PHP

When ldap is installed and enabled, phpinfo() will display ldap section:

For an example of how to use PHP LDAP functionality to search Windows Active Directory check here.

Источник

Оцените статью