PHP Active Directori LDAP login


<?php
$u="username";
$p="password";
$ad_server = "202.xxx.yyy.zzz"; //"jatimdc.jatim.corp.pln.co.id";
$ad_dn    = 'DC=pln-enjiniring,DC=com';                // Domain DN
$ad_usn_postfix    = '@pln-enjiniring.com';
if (!empty($u) && !empty($p)) //Jika username ada isinya maka diproses jika tidak ada langsung ke form login
{
/*     ==== Cek pada LDAP dulu.
==== Kalo LDAP tidak bisa terkoneksi, konfirmasi user memakai database
*/
$userConfirm = false; //nilai default user sudah terkonfirm atau belum
$loginByDB = false; //nilai default kalo2 tidak bisa login lewat AD

//Connect to Active Directory
$ad = ldap_connect($ad_server);
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);

$adusn = $u.$ad_usn_postfix; $adpwd = $p;


$bind = @ldap_bind($ad, $adusn, $adpwd);
error_reporting();
$ldapErr = ldap_errno($ad);

if ($ldapErr==0)    //JIKA LDAP LOGIN BERHASIL
{
//Ambil data nama dari active directory
$ldap_search_param = "(&"."(sAMAccountName=$u)".")";
$ldap_search_return = array('displayname','employeenumber','mail','company','department','title');

$search = ldap_search($ad, $ad_dn, $ldap_search_param, $ldap_search_return);
$entries = ldap_get_entries($ad, $search);

/*$AD_displayName = substr(str_replace("'","\'",$entries[0]['displayname'][0]),0,150);
$AD_employeeNumber = substr(str_replace("'","\'",$entries[0]['employeenumber'][0]),0,14);
$AD_mail = substr(str_replace("'","\'",$entries[0]['mail'][0]),0,150);
$AD_company = substr(str_replace("'","\'",$entries[0]['company'][0]),0,150);
$AD_department = substr(str_replace("'","\'",$entries[0]['department'][0]),0,150);
$AD_title = substr(str_replace("'","\'",$entries[0]['title'][0]),0,150);*/
}
else{
echo "Gagal login";
}
}
?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: