|
 |
|
ขอตัวอย่างหรือวิธีการเขียน PHP จัดการข้อมูลใน AD(Active Directory) ใน Server ครับ |
|
 |
|
|
 |
 |
|
ตอนนี้คิดว่า เชื่อมต่อได้แล้วด้วย Code
Code (PHP)
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=tis-620">
<?php
// $username = $_POST['username'];
// $pass = $_POST['password'];
$username = "admin";
$pass = "admin";
if($username !=null and $pass !=null)
{
$server = "10.10.10.10";
$user = $username."@test.org";
// connect to active directory
$ad = ldap_connect($server);
if(!$ad) {
die("Connect not connect to ".$server);
echo "ไม่สามารถติดต่อ server ได้";
exit();
} else {
$b = @ldap_bind($ad,$user,$pass);
if(!$b) {
die("<br><br>
<div align='center'> ท่านกรอกรหัสผ่านผิดพลาด
<br>
</div>
<meta http-equiv='refresh' content='3 ;url=index.php'>");
} else {
//login ผ่านแล้วมาทำไรก็ว่าไป
session_start();
echo "เชื่อมต่อได้แล้ว";
}
}
}
?>
ตอนนี้มันขึ้น "เชื่อมต่อได้แล้ว" แล้ว แต่ผมติดที่จะเข้าไปจัดการข้อมูล เช่นการเข้าไปเปลี่ยน Password หรือ แค่แสดงข้อมูล
ผมลองแก้ Code แบบข้างล่างดูแต่ก็ยังไม่ได้ครับ
Code (PHP)
function ldap_login($ds,$username,$password,$ou,$suffix){
$i=0;
while(1){
$binddn = "uid=$username,ou=".$ou[$i].",".$suffix;
@$ldapbind = ldap_bind($ad, $binddn, $password);
if ($ldapbind){
return true;
break;
}else{
if($i==3){
ldap_close($ds);
return false;
break;
}
}
$i++;
}
}
$ou = array("student","staff","unistaff");
$suffix_string = "dc=psu,dc=pn";
$USER = "3114";
$PASSWORD = "sanji007";
if(ldap_login($ad,$USER,$PASSWORD,$ou,$suffix_string)){
echo "สวัสดีครับ";
}else{
echo "User หรือ Password ผิด";
}
โปรดช่วยแนะนำหน่อยครับ
|
 |
 |
 |
 |
Date :
2018-03-22 16:23:43 |
By :
yutthanagorn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จากที่ผมพยายาม ศึกษาดูแล้วนะครับ คือยังไม่เข้าใจ โดยผมใช้ จาก ตัวอย่างนี้ https://www.thaicreate.com/php/php-user-authentication-active-directory.html ซึ่งก็สามารถเชื่อมต่อได้ไปยัง Server ได้ แต่ยังไม่เข้าใจการที่จะดึงข้อมูลเอาโชว์ เพิ่ม หรือแก้ไข โดยเป้าหมายหลักๆคือ อยากแค่ เปลี่ยน Password ให้ได้
Code (PHP)
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=tis-620">
<?php
// $username = $_POST['username'];
// $pass = $_POST['password'];
$username = "admin";
$pass = "admin";
if($username !=null and $pass !=null)
{
$server = "10.10.10.10";
$user = $username."@test.org";
// connect to active directory
$ad = ldap_connect($server);
if(!$ad) {
die("Connect not connect to ".$server);
echo "ไม่สามารถติดต่อ server ได้";
exit();
} else {
$b = @ldap_bind($ad,$user,$pass);
if(!$b) {
die("<br><br>
<div align='center'> ท่านกรอกรหัสผ่านผิดพลาด
<br>
</div>
<meta http-equiv='refresh' content='3 ;url=index.php'>");
} else {
//login ผ่านแล้วมาทำไรก็ว่าไป
session_start();
echo "เชื่อมต่อได้แล้ว";
}
}
}
?>
ท่ั้งนี้ผมลองตาม Link https://www.thaicreate.com/community/ldap2.html ซึ่งก็มีข้อสงสัยหลายอย่างมากที่ไม่ใจ พยายามทำตามทีละ Step by step แล้วเช่น หัวข้อที่ 3 $ds=ldap_connect(?localhost?,?389?) ทำไมต้องมี ? แล้วพอใส่ไปก็ error ตรง ?
หรือตาม code
Code (PHP)
<?php
function ldap_login($ds,$username,$password,$ou,$suffix){
$i=0;
while(1){
$binddn = "uid=$username,ou=".$ou[$i].",".$suffix;
@$ldapbind = ldap_bind($ds, $binddn, $password);
if ($ldapbind){
return true;
break;
}else{
if($i==3){
ldap_close($ds);
return false;
break;
}
}
$i++;
}
}
$ds=ldap_connect("10.10.10.10");
$USER="administrator";
$PASSWORD="Password";
$ou = array("student","staff","unistaff");
$suffix_string = "dc=imlab,dc=org";
if(ldap_login($ds,$USER,$PASSWORD,$ou,$suffix_string)){
echo "สวัสดีครับ";
}else{
echo "User หรือ Password ผิด";
}
?>
ซึ่งผมไม่เข้าใจความหมายมันครับ เช่น
ตรง $binddn = "uid=$username,ou=".$ou[$i].",".$suffix; uid,ou คืออะไร
ตรง @$ldapbind = ldap_bind($ds, $binddn, $password); ทำไมต้องมี @ พอใส่ก้ error ไม่ใส่ก็ error แบบอื่น
ตรง $ou = array("student","staff","unistaff"); คือค่าอะไร
ตรง "dc=imlab,dc=org"; ผมเข้าใจถูกใช่ไหมว่าเป็น Domain
แล้วพอไปอ่านตัวอย่างต่อไป ก็ยิ่ง งง ค่าเข้าไปอีกใครพอจะมี Code ตัวอย่างที่เข้าใจง่ายหรืออธิบายดีๆหน่อยไหมครับ
|
 |
 |
 |
 |
Date :
2018-03-27 18:57:15 |
By :
yutthanagorn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|