如何使用PHP获取ldap中的授权用户?

时间:2017-02-24 15:27:48

标签: php ldap

我想将我的系统登录密码用于php登录页面。所以我在我的项目中使用了LDAP概念。我在下面提到了我的编码,就是一切都很好。但是当我运行此代码时,结果显示“无效用户”。我不知道为什么这是错误的。

$ldaphost = 'abc.co.in';
$ldapport = '389';

$username = '4444';
$password = '4444pass';

$ldap = ldap_connect($ldaphost, $ldapport)
     or die("Could not connect to $ldaphost");

ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);

$user = "uid=$username,dc=abc,dc=co,dc=in";

$bind = @ldap_bind($ldap, $user, $password);

if ($bind) {
    echo "<br />Valid user";
} else {
    $msg = "<br />Invalid user";
    echo $msg;
}

结果下方:

enter image description here

我的代码有什么问题,或者我需要添加什么?

请找到并解决此请求。这对我更有帮助。

谢谢你提前......

1 个答案:

答案 0 :(得分:0)

This is how my ldap thing works. change your ldap host to be either "ldap://abd.asd.co:389' or "ldaps://asd.basd.co:636".

function verify_user() {


   $user = $_REQUEST['user'];
   $passwd = $_REQUEST['pass'];

   // Bind to LDAP to check is user is valid
   $server = "ldaps://ldap.server.com:636";
   $dn = "uid=$user, ou=People, ou=something, dc=other, dc=whatever";
   // Create a fake password if needed to keep people from anonymously
   // binding to LDAP
   if($passwd == '') { $passwd = "p"; }
   $ldap = ldap_connect($server) or die("Can't connect to LDAP server!");
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_start_tls($ldap);
   if($ldap) {
      $bnd = @ldap_bind($ldap, $dn, stripslashes($passwd));
      if(!$bnd)  {
         sleep(5);
         echo "<br>Error: Bad Username or Password!<br>";
         exit;
      }
   }

   header("Location: {$_REQUEST['url']}"); /* Redirect browser */
   exit;
}