php 新增 LDAP 使用者
<html xmlns="HTTP://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta HTTP-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title></title>
</head>
<body>
<!-----------------------新增資料---------------------------------------------------------------------------------------------------------------------------------------------->
<?php
if(isset($_POST) && array_key_exists('an',$_POST)){
$add_cn = $_POST['cn']; //使用者名
$add_pw = $_POST['pw']; //密碼
$add_mail = $_POST['mail'];
$add_sn = $_POST['sn'];
$add_gn = $_POST['gn'];
$add_uid = $_POST['uid'];
$add_oc = $_POST['oc'];
$an = $_POST['an'];//送出判斷
}
if(!empty($an)){
//if( empty($add_cn) or empty($add_pw) or empty($add_mail) or empty($add_sn) or empty($add_gn) or empty($add_uid) or empty($add_oc)){
if( empty($add_cn) or empty($add_pw) or empty($add_mail) or empty($add_sn) or empty($add_gn) or empty($add_uid)){
$msg = "一些表格空的!";
}else{
//連到AD Server的帳號密碼
$account="cn=Manager,dc=ntpu,dc=edu,dc=tw";//root帳號 要新增資料
$password="1";
//連線到AD server
$ldap_host = "xxx.xxx.xxx.xxx";
$ldap_port = 389;
$base_dn = "dc=ntpu,dc=edu,dc=tw";
$conn = ldap_connect( $ldap_host, $ldap_port); //連接伺服器
//以下兩行務必加上,否則AD無法在不指定OU下,作搜尋的動作
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
//連線bind帳號密碼
$ldap_bd=ldap_bind($conn,$account,$password);
//檢查帳號密碼是否正確
if ($ldap_bd) {
//要新增的資料
$info["cn"]=$add_cn;
$info["userpassword"]= "{SHA}".base64_encode(pack("H*",sha1($add_pw)));
$info["mail"]=$add_mail;
$info["sn"]=$add_sn;
$info["givenname"]=$add_mail;
$info["uid"]=$add_uid;
$info["objectclass"] ="inetOrgPerson";
$insert=ldap_add($conn, "cn=".$add_cn.",ou=oh01,dc=ntpu,dc=edu,dc=tw", $info);
if($insert)
{
echo "新增成功";
echo $info;
}else{
echo "fail";
}
}else{
echo "Auth failed!"."<br>";
}
//-----------------------------搜尋用*------------------------------------------------
/*
$cn="cn=*";
$search=ldap_search($conn,$base_dn,$cn);//根據cn獲取到使用者的資訊
$info = ldap_get_entries($conn, $search);
//搜尋的全部資料
echo "資料傳回 ".$info["count"]."筆:<p>";
for ($i=0; $i<$info["count"]; $i++) {
echo "dn:". $info[$i]["dn"] ."<br>"; //顯示dn
echo "cn:". $info[$i]["cn"][0] ."<br>"; //顯示cn
echo "mail:". $info[$i]["mail"][0] ."<br>"; //顯示mail
echo "uid:". $info[$i]["uid"][0] ."<br>"; //顯示uid
echo "userpassword:". $info[$i]["userpassword"][0] ."<br>"; //顯示userpassword
echo "sn:". $info[$i]["sn"][0] ."<br>";
echo "givenname:". $info[$i]["givenname"][0] ."<br>";
echo "objectclass :". $info[$i]["objectclass"][0] ."<p>";
}
echo '<pre>';
var_dump($info);
echo '</pre>';
*/
//---------------------------------------------------------------------------------------
ldap_close($conn);// 關閉連接
}
}
if(!empty($msg)){
print("<h1>$msg</h1>");
}
?>
<!--------------------------------------------------------------------------------------->
<form method="post" action="">
<dl>
<dt>帳號/cn</dt>
<dd><input type="text" name="cn" /></dd>
<dt>密碼/userpassword</dt>
<dd><input type="password" name="pw" /></dd>
<dt>信箱/mail</dt>
<dd><input type="email" name="mail" /></dd>
<dt>Last name/sn</dt>
<dd><input type="text" name="sn" /></dd>
<dt>Given Name/givenname</dt>
<dd><input type="text" name="gn" /></dd>
<dt>User ID/uid</dt>
<dd><input type="text" name="uid" /></dd>
<!--<dt>objectclass/objectclass</dt>
<dd><input type="text" name="oc" /></dd>-->
<dd><input type="submit" value="確定" /></dd>
</dl>
<input type="hidden" name="an" value="submit" />
</form>
</body>
</html>
<meta HTTP-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title></title>
</head>
<body>
<!-----------------------新增資料---------------------------------------------------------------------------------------------------------------------------------------------->
<?php
if(isset($_POST) && array_key_exists('an',$_POST)){
$add_cn = $_POST['cn']; //使用者名
$add_pw = $_POST['pw']; //密碼
$add_mail = $_POST['mail'];
$add_sn = $_POST['sn'];
$add_gn = $_POST['gn'];
$add_uid = $_POST['uid'];
$add_oc = $_POST['oc'];
$an = $_POST['an'];//送出判斷
}
if(!empty($an)){
//if( empty($add_cn) or empty($add_pw) or empty($add_mail) or empty($add_sn) or empty($add_gn) or empty($add_uid) or empty($add_oc)){
if( empty($add_cn) or empty($add_pw) or empty($add_mail) or empty($add_sn) or empty($add_gn) or empty($add_uid)){
$msg = "一些表格空的!";
}else{
//連到AD Server的帳號密碼
$account="cn=Manager,dc=ntpu,dc=edu,dc=tw";//root帳號 要新增資料
$password="1";
//連線到AD server
$ldap_host = "xxx.xxx.xxx.xxx";
$ldap_port = 389;
$base_dn = "dc=ntpu,dc=edu,dc=tw";
$conn = ldap_connect( $ldap_host, $ldap_port); //連接伺服器
//以下兩行務必加上,否則AD無法在不指定OU下,作搜尋的動作
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
//連線bind帳號密碼
$ldap_bd=ldap_bind($conn,$account,$password);
//檢查帳號密碼是否正確
if ($ldap_bd) {
//要新增的資料
$info["cn"]=$add_cn;
$info["userpassword"]= "{SHA}".base64_encode(pack("H*",sha1($add_pw)));
$info["mail"]=$add_mail;
$info["sn"]=$add_sn;
$info["givenname"]=$add_mail;
$info["uid"]=$add_uid;
$info["objectclass"] ="inetOrgPerson";
$insert=ldap_add($conn, "cn=".$add_cn.",ou=oh01,dc=ntpu,dc=edu,dc=tw", $info);
if($insert)
{
echo "新增成功";
echo $info;
}else{
echo "fail";
}
}else{
echo "Auth failed!"."<br>";
}
//-----------------------------搜尋用*------------------------------------------------
/*
$cn="cn=*";
$search=ldap_search($conn,$base_dn,$cn);//根據cn獲取到使用者的資訊
$info = ldap_get_entries($conn, $search);
//搜尋的全部資料
echo "資料傳回 ".$info["count"]."筆:<p>";
for ($i=0; $i<$info["count"]; $i++) {
echo "dn:". $info[$i]["dn"] ."<br>"; //顯示dn
echo "cn:". $info[$i]["cn"][0] ."<br>"; //顯示cn
echo "mail:". $info[$i]["mail"][0] ."<br>"; //顯示mail
echo "uid:". $info[$i]["uid"][0] ."<br>"; //顯示uid
echo "userpassword:". $info[$i]["userpassword"][0] ."<br>"; //顯示userpassword
echo "sn:". $info[$i]["sn"][0] ."<br>";
echo "givenname:". $info[$i]["givenname"][0] ."<br>";
echo "objectclass :". $info[$i]["objectclass"][0] ."<p>";
}
echo '<pre>';
var_dump($info);
echo '</pre>';
*/
//---------------------------------------------------------------------------------------
ldap_close($conn);// 關閉連接
}
}
if(!empty($msg)){
print("<h1>$msg</h1>");
}
?>
<!--------------------------------------------------------------------------------------->
<form method="post" action="">
<dl>
<dt>帳號/cn</dt>
<dd><input type="text" name="cn" /></dd>
<dt>密碼/userpassword</dt>
<dd><input type="password" name="pw" /></dd>
<dt>信箱/mail</dt>
<dd><input type="email" name="mail" /></dd>
<dt>Last name/sn</dt>
<dd><input type="text" name="sn" /></dd>
<dt>Given Name/givenname</dt>
<dd><input type="text" name="gn" /></dd>
<dt>User ID/uid</dt>
<dd><input type="text" name="uid" /></dd>
<!--<dt>objectclass/objectclass</dt>
<dd><input type="text" name="oc" /></dd>-->
<dd><input type="submit" value="確定" /></dd>
</dl>
<input type="hidden" name="an" value="submit" />
</form>
</body>
</html>
留言
張貼留言