search
Homephp教程php手册linux ldap认证:windos 和 linux 下实现PHP和LDAP身份认证


我现在的老板曾要求我为企业内部互联网的Web服务提供一种标准的身份验证方法。我遇到的一个主要问题就是我们公司主要使用了两种平台:UNIX和Windows。所以,我的第一个想法并不很成功:它要求每个员工都使用UNIX或者Linux而放弃Windows。
我认为解决现在的UNIX/Windows问题的最好方法就是利用PHP的LDAP特性。由于LDAP服务器,要求我使用现有的系统,主要指的是一个巨大的Microsoft Exchange Server系统。我非常高兴使用Exchange,它很可靠,而且LDAP特性的使用和配置也极为简单。不过,请你注意:这套方案就身份验证角度来看并不是最安全的。如果有较高等级的安全需求,我强烈建议你采用LDAP和SSL。
从哪里开始学习
为了让你入门,我给出了一个PHP LDAP函数的清单并对函数的功能给以简要说明。然后,我将演示如何建立到LDAP服务器的连接并验证用户。为了代码简单起见,我将演示PHP连接的功能以及如何绑定到LDAP服务器。
一对绝配:PHP和LADP
下面是我在例子中将要使用的函数的清单。网上有相关资料。
ldap_connect—用来连接LDAP服务。
ldap_bind—用来绑定到特定的LDAP目录。
ldap_error—从LDAP服务器上获得错误信息。
ldap_search—用来开始搜索。
ldap_get_entries—从搜索结果中获得多个结果。
ldap_close—关闭LDAP连接。
现在我在例子中演示如何使用第一个函数(代码清单A)并适当介绍该函数的功能。
// LDAP variables
$ldap[‘user’] = ‘uname’;
$ldap[‘pass’] = ‘password’;
$ldap[‘host’] = ‘ldap.example.com’;
$ldap[‘port’] = 389;
$ldap[‘dn’] = ‘cn’.$ldap[‘user’].’,ou=Department,o=Company Name’;
$ldap[‘base’] = ‘’;
// connecting to ldap
$ldap[‘conn’] = ldap_connect( $ldap[‘host’], $ldap[‘port’] )
or die( “Could not connect to {$ldap[‘host’]}” );
?>
将会返回一个到LDAP服务器的连接(也称为资源,即resource)。ldap_connect函数有两个参数:主机(host)和端口。第一个参数:主机就是LDAP主机名称,第二个参数是LDAP运行的端口。默认情况下,LDAP使用的端口号为389。如果你需要到LDAP服务器的一个安全连接,你可以把参数host改为一个你可以访问的LDAP服务器的URL,如下所示:
$ldap[‘conn’] = ldap_connect( “ldaps://ldap.example.com” );
由于你指定了URL而不是服务器名称,在这种方法下,你就不需要使用端口参数了。需要牢记的一点就是确切名称需要与加密套接字协议层证书(the SSL certificate)对应。
// LDAP variables
$ldap[‘user’] = ‘uname’;
$ldap[‘pass’] = ‘password’;
$ldap[‘host’] = ‘ldap.example.com’;
$ldap[‘port’] = 389;
$ldap[‘dn’] = ‘cn’.$ldap[‘user’].’,ou=Department,o=Company Name’;
$ldap[‘base’] = ‘’;
// connecting to ldap
$ldap[‘conn’] = ldap_connect( $ldap[‘host’], $ldap[‘port’] )
or die( “Could not connect to {$ldap[‘host’]}” );
// binding to ldap
$ldap[‘bind’] = ldap_bind( $ldap[‘conn’], $ldap[‘dn’], $ldap[‘pass’] );
?>
演示了如何用用户名和口令来绑定到服务器。我创建了一个合适的域名(domain name ,DN)并用用户的口令来合法连接到LDAP。我们通过使用域名和口令就可以让LDAP服务器通过身份认证并允许绑定连接,这样我们就成功的绑定上了。ldap_bind的返回值是一个布尔类型。我们可以根据返回值判断用户的登录证书是否有效。当这个过程结束后,你就可以知道用户身份是否得到了认证。
如果发生了错误会怎样?调用ldap_error函数是判断发生了什么错误的好方法。ldap_error函数返回了一个字符串,其中包含了LDAP服务器发生的最后错误的信息。

// LDAP variables
$ldap[‘user’] = ‘uname’;
$ldap[‘pass’] = ‘password’;
$ldap[‘host’] = ‘ldap.example.com’;
$ldap[‘port’] = 389;
$ldap[‘dn’] = ‘cn’.$ldap[‘user’].’,ou=Department,o=Company Name’;
$ldap[‘base’] = ‘’;
// connecting to ldap
$ldap[‘conn’] = ldap_connect( $ldap[‘host’], $ldap[‘port’] )
or die( “Could not connect to server {$ldap[‘host’]} );
// binding to ldap
$ldap[‘bind’] = ldap_bind( $ldap[‘conn’], $ldap[‘dn’], $ldap[‘pass’] );
if( !$ldap[‘bind’] )
{
echo ldap_error( $ldap[‘conn’] );
exit;
}
?>
中,我向脚本中添加了ldap_error函数,如果绑定到LDAP服务器的用户身份没有得到确认,那么代码将退出运行。该函数返回一个字符串,该字符串包含了发送到LDAP服务器的最后一条指令产生的错误信息。如果你按给定用户名和口令的绑定没有成功登录,那么错误信息将包含这对无效的用户名和口令。
在我们的最后一个例子中
// LDAP variables
$ldap[‘user’] = ‘uname’;
$ldap[‘pass’] = ‘password’;
$ldap[‘host’] = ‘ldap.example.com’; 本文链接http://www.cxybl.com/html/wlbc/Php/20121126/34395.html



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.