0

我正在编写一个 PHP 应用程序来从 MySQL 数据库创建 Active Directory 用户。我可以让它工作到一定程度,但是当我尝试动态设置 DN 或 CN 时,会出现语法错误。警告:ldap_add(): Add: Invalid DN syntax in on line 44。有问题的行是我连接和创建用户的位置。如果我静态设置 CN 和 DN,它会完美运行。下面是我的代码。我很想听听让它更好地工作的方法,因为我对 PHP 编程还是很陌生..

 <?php
ini_set('display_errors',1);
error_reporting(E_ALL);

//variables
$lastname=$_POST["LastName"];
$firstname=$_POST["FirstName"];
$middlename=$_POST["MiddleName"];
$empid=$_POST["empid"];

$samaccount=$firstname.'.'.$lastname;
$displayname = $lastname.', '.$firstname;

$domain = "domain.lan";
$ldap_dn ="CN=Users,DC=domain,DC=lan";
$ldap_usr_dom = "@".$domain;

//end variables
$ldapConn = ldap_connect('ldap://PDC:389');
ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_bind( $ldapConn, 'CN=administrator,CN=Users,DC=domain,DC=lan', 'pw');
//$dn ="test",$ldap_dn;

//this line and one below I cannot set with any variable
$dn_user='CN=$lastname",CN=Users,DC=domain,DC=lan';

$ldaprecord['cn'] = "$lastname";
$ldaprecord['givenName'] = $firstname;  
$ldaprecord['sn'] = $lastname;
$ldaprecord['sAMAccountName'] = $samaccount;
$ldaprecord['UserPrincipalName'] = "$samaccount$ldap_usr_dom";
$ldaprecord['displayName'] = $displayname;
$ldaprecord['UserAccountControl'] = "544";
$ldaprecord['objectclass'][0] = 'top';
$ldaprecord['objectclass'][1] = 'person';
$ldaprecord['objectclass'][2] = 'organizationalPerson';
$ldaprecord['objectclass'][2] = 'user';
$ldaprecord['mail'] =  "$samaccount$ldap_usr_dom";
$ldaprecord['employeeID'] =  "$empid";

ldap_add($ldapConn, $dn_user, $ldaprecord);
ldap_unbind($ldapConn);  
echo "request processed";

?>
4

0 回答 0