|
|
|
สอบถามเรื่อง Login โดยการใช้ LDAP บาง Users ไม่สามารถ bind ได้ |
|
|
|
|
|
|
|
ขอดู code หน่อยครับ
|
|
|
|
|
Date :
2018-12-17 14:56:23 |
By :
mongkon.k |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?php
ob_start();
session_start();
//error_reporting(0);
?>
<?php
function ddd() {
date_default_timezone_set("Asia/Bangkok");
$currentdate = date('j/M/Y H:i:s');
$user = $_REQUEST['username'];
$password = $_REQUEST['password'];
$ldap_dn = "OU=xx, OU=xx, DC=xx, DC=xx, DC=local";
$ldap_con = ldap_connect("xxxxxxxxxxxxxxxxxxxx");
$ldap_user = $user.'@xxxxxxxxx';
$ldap_password = $password;
$ldap_bind = @ldap_bind($ldap_con, $ldap_user, $ldap_password);
ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_con, LDAP_OPT_REFERRALS, 0 );
if (!$ldap_bind){
echo '<script language="javascript">';
echo 'alert("Wrong username or password!! Please try again.")';
echo '</script>';
ldap_errno( $ldap_con );
include("database_connect.php");
$query = "INSERT INTO LoginLog (username, name, timelogin, status) VALUES (?, ?, ?, ?)";
$params = array($user, 'unknow', $currentdate, 'failed, wrong password');
if (sqlsrv_query($conn, $query, $params)) {
// echo "Statement executed.\n";
echo "<meta http-equiv='refresh' content='0;URL=pcipatching.php' />";
} else {
echo "Error in statement execution.\n";
die(print_r(sqlsrv_errors(), true));
echo "<meta http-equiv='refresh' content='0;URL=pcipatching.php' />";
}
//return $ldap;
//echo "<meta http-equiv='refresh' content='0;URL=login_form.php?username=".$_REQUEST['username']."' />";
//echo "invalid credential!!!";
//die("<br><br>");
}else if ($ldap_bind) {
$filter = "(sAMAccountName=$user)";
$result = ldap_search($ldap_con, $ldap_dn, $filter);
$entries = ldap_get_entries($ldap_con, $result);
for ($i=0; $i<$entries["count"]; $i++) {
//echo "Authenticated<br>";
//echo "<p>Welcome <strong> ". $entries[$i]["cn"][0] ."</strong><br /> Username: " . $entries[$i]["samaccountname"][0] ."<br>".$entries[$i]["memberof"][0]."</p>\n";
$mail = $entries[$i]["mail"][0];
$realname = $entries[$i]["cn"][0];
foreach($entries[0]['memberof'] as $grps) {
// is user
if(strpos($grps, "group1") ) $access = 0;
if(strpos($grps, "group2") ) $access = 1;
if(strpos($grps, "group3") ) $access = 2;
echo '<pre>';
//var_dump($grps);
echo '</pre>';
}
}
if($access == 1) {
echo "Access done<br>";
// establish session variables
$_SESSION['realname'] = $realname;
$_SESSION['mail'] = $mail;
$_SESSION['username'] = $user;
$_SESSION['access'] = $access;
echo $user."<br>";
//echo $_SESSION['mail'];
//echo $_SESSION['access'];
echo $realname."<br>";
echo $currentdate."<br>";
include("database_connect.php");
$query = "INSERT INTO LoginLog (username, name, timelogin, status) VALUES (?, ?, ?, ?)";
$params = array($user, $realname, $currentdate, 'success');
if (sqlsrv_query($conn, $query, $params)) {
echo "Statement executed.\n";
// echo "<meta http-equiv='refresh' content='0;URL=pcipatching.php' />";
} else {
echo "Error in statement execution.\n";
die(print_r(sqlsrv_errors(), true));
}
header("location: homepage.php");
//return $ldap;
}
if($access == 2) {
echo "Access done<br>";
// establish session variables
$_SESSION['realname'] = $realname;
$_SESSION['mail'] = $mail;
$_SESSION['username'] = $user;
$_SESSION['access'] = $access;
echo $user."<br>";
//echo $_SESSION['mail'];
echo $_SESSION['access'];
echo $realname."<br>";
echo $currentdate."<br>";
include("database_connect.php");
$query = "INSERT INTO LoginLog (username, name, timelogin, status) VALUES (?, ?, ?, ?)";
$params = array($user, $realname, $currentdate, 'success');
if (sqlsrv_query($conn, $query, $params)) {
echo "Statement executed.\n";
// echo "<meta http-equiv='refresh' content='0;URL=pcipatching.php' />";
} else {
echo "Error in statement execution.\n";
die(print_r(sqlsrv_errors(), true));
}
header("location: homepage.php");
//return $ldap;
}
if ($access <= 0) {
echo '<script language="javascript">';
echo 'alert("You have no permission to access this website!")';
echo '</script>';
include("database_connect.php");
$query = "INSERT INTO LoginLog (username, name, timelogin, status) VALUES (?, ?, ?, ?)";
$params = array($user, 'unknow', $currentdate, 'failed, no permission');
if (sqlsrv_query($conn, $query, $params)) {
echo "Statement executed.\n";
// echo "<meta http-equiv='refresh' content='0;URL=pcipatching.php' />";
} else {
echo "Error in statement execution.\n";
die(print_r(sqlsrv_errors(), true));
}
//return $ldap;
echo "<meta http-equiv='refresh' content='0;URL=login_form.php?username=".$_REQUEST['username']."' />";
//return false;
}
//ldap_close($ldap_con);
}
}
ddd();
?>
ปีกกาอาจจะผิดไปบ้างนะครับในนี้ไม่ต้องสนใจ แต่ในโค้ดจริงถูกแน่ครับ
login แล้วเด้งเข้าตั้งแต่บรรทัด 25เลยครับ bind ไม่เจอ
ทั้งๆที่แต่ก่อนก็เข้าได้ปกติครับ
ส่วน account ที่เข้าได้ปกติถึงตอนนี้ก็มีครับ ปกติทุกอย่างเข้าเว็บได้
ที่เข้าไม่ได้ตอนนี้แค่2-3คน
|
ประวัติการแก้ไข 2018-12-17 17:44:45 2018-12-17 17:45:40
|
|
|
|
Date :
2018-12-17 17:43:21 |
By :
nookseal |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
รบกวนด้วยครับผม ยังแก้ไม่ได้เลย
|
|
|
|
|
Date :
2018-12-24 09:33:42 |
By :
nookseal |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
หลักการ include file import file etc... ต่าง ควรไว้บนสุด ครั้งเดี่ยวจบครับ ไม่ต้องเอามาเยอะ
Code เขียนเกือบถูกหมด แต่ผิด Process
ทำไม ทุกๆครั้ง เวลาเข้าระบบ ต้องบันทืกการเข้าระบบทุกครั้ง?
ขอเดาว่า ทุกๆครั้ง ที่ Run หน้านี้ มันจะทำงาน 25-28 บรรทัด แบบนี้
เพราะ if(!...) {
..... นี้ไง มันเข้าเงื่อนไขนี้ ทุกครั้ง มันก็แสดง java script alert มาทุกครั้ง
}
ไปหาวิธีที่ดีกว่านี้ก็ได้
Code ถูกหมด แต่ว่าง Process ผิดๆ ส่วน Code ที่ Function ถูกก็ไม่ทำงานนะครับ
แนะนำเฉยๆ
|
|
|
|
|
Date :
2018-12-24 14:13:23 |
By :
Hararock |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
ึืคือที่สงสัยก็เพราะว่า users ส่วนใหญ่นั้นเข้ากันได้ครับจนถึงตอนนี้ก็เข้าได้ปกติรวมถึง account ผมด้วย
มีเพียง2-3 คนที่เข้าไม่ได้ แล้วไม่ใช่ว่าเข้าไม่ได้ตั้งแต่แรกนะครับ เคยเข้าได้แล้วจู่ๆก็มาเข้าไม่ได้
จากที่ลอง debug เบื้องต้นก็คือติดที่ bind ไม่เจอและเด้งเข้า if (!bind) บรรทัดที่ 25 ครับ
คือถ้าเข้าไม่ได้ทุกคนเนี่ย จะไม่สงสัยเลยครับผม
|
|
|
|
|
Date :
2018-12-24 16:11:21 |
By :
nookseal |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|