PHP ldap_add 函数转义 DN 语法中的 ldap 特殊字符
我正在尝试将一些用户添加到我的 Ldap DB,但是当我使用一些特殊字符(例如“,.”)时,我收到一些错误(无效的 dn 语法)。我需要一个转义所有字符的函数。我尝试 preg_quote 但在某些情况下会出现一些错误。
提前致谢
代码:
$user = 'Test , Name S.L';
if(!(ldap_add($ds, "cn=" . $user . ",".LDAP_DN_BASE, $info))) {
include 'error_new_account.php';
}
I'm trying to add some users to my Ldap DB but I get some errors (invalid dn syntax) when I use some special characters like ",.". I need a function that escape all characters. I try preg_quote but I get some errors in some cases.
Thanks in advance
Code:
$user = 'Test , Name S.L';
if(!(ldap_add($ds, "cn=" . $user . ",".LDAP_DN_BASE, $info))) {
include 'error_new_account.php';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑 2013 年 1 月:根据 RFC 4514。感谢 Eugenio< /a> 指出这个问题。
编辑 2014 年:我将此函数添加到 PHP 5.6。下面的代码现在是早期 PHP 版本的类似替代品。
所以你会这样做:
EDIT Jan 2013: added support for escaping leading/trailing spaces in DN strings, per RFC 4514. Thanks to Eugenio for pointing out this issue.
EDIT 2014: I added this function to PHP 5.6. The code below is now a like-for-like drop-in replacement for earlier PHP versions.
So you would do:
PHP 5.6 Beta 发布
ldap_escape()
函数最近已经生效,但是,该版本目前还没有准备好用于生产,目前您可以将其用于您的开发目的。PHP 5.6 Beta released
ldap_escape()
function recently and it is in effect, However, this version is not production ready at present, you can very use it for your development purposes as of now.请注意,如果您还没有使用 PHP 5.6,您可以使用我在下面创建的方法来镜像确切的 PHP 5.6 函数 ldap_escape(),请记住,这是在类中使用的。上面的答案与 ldap_escape 函数的执行方式并不完全相同,因为如果没有给出标志,它不会将所有字符转义为十六进制字符串,因此这更适合插入以面向对象的方式替代早期版本的 PHP。
我记录了每一行,以便更容易理解正在发生的事情。向下滚动查看输出。
方法(与 PHP 5 或更高版本兼容):
测试(请注意,LDAP_ESCAPE 常量仅在 PHP 5.6 中可用):
Github Gist 链接:https://gist.github.com/stevebauman/0db9b5daa414d60fc266
Just a heads up if your not on PHP 5.6 yet, you can mirror the exact PHP 5.6 function
ldap_escape()
using the methods I created below, keep in mind this is meant for use in a class. The above answer doesn't perform exactly like theldap_escape
function, as in it doesn't escape all characters into a hex string if no flags have been given, so this would be more suitable for a drop in replacement for earlier versions of PHP, in an object oriented way.I've documented every line for an easier understanding on whats going on. Scroll down for output.
Methods (Compatible with PHP 5 or greater):
Tests (be aware that LDAP_ESCAPE constants are only available in PHP 5.6):
Github Gist link: https://gist.github.com/stevebauman/0db9b5daa414d60fc266
这些字符必须转义为专有名称或相对专有名称的数据的一部分。使用反斜杠 2 个十六进制数字转义字符(与所有 LDAP 中一样),例如
\2a
。其他任何内容都不符合标准主体文件。有关可分辨名称的字符串表示形式的更多具体信息,请参阅 RFC4514。Those characters must escaped to be part of the data of a distinguished name or relative distinguished name. Escape the character (as in all LDAP) with a backslash 2 hex digit, such as
\2a
. Anything else would not be in compliance with the standards body documents. See RFC4514 for more specific information regarding the string representation of distinguished names.