LDAP验证失败时无法显示模式弹出窗口

时间:2019-05-15 12:52:35

标签: php ldap bootstrap-modal

我正在努力通过PHP从LDAP验证用户。我尝试了一些代码,现在可以完美验证用户了。但是,如果登录失败,我想显示带有一些自定义消息的模式弹出窗口。这是我的代码-

<?php


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // username and password sent from form 

    $username = $_POST['username'];
    //print($username);
    $password = $_POST['password'];
    //print($password);
    // using ldap bind

    $ldaprdn  = 'uid=' .$username. ',ou=People,ou=AP,o=abcd.com'; // ldap rdn or dn
    $ldappass = $password;

    // connect to ldap server
    $ldapconn = ldap_connect("ad.abcd.com") or die("Could not connect to LDAP server.");

    if ($ldapconn) {

        try {
    $ldapbind = @ldap_bind($ldapconn, $ldaprdn, $ldappass);
} catch (Exception $e) {

    //echo "<script type='text/javascript'>$('#myModal').modal('show');</script>";
                //echo "<script type='text/javascript'>$('#myModal').fadeIn('show');</script>";
                echo "<script>
         $(window).load(function(){
             $('#myModal').modal('show');
         });
    </script>";
}
        if ($ldapbind) {

            $filter = '(sAMAccountName='.$username.')';
            $result = ldap_search($ldapconn, $ldaprdn, "(cn=*)") or exit("Unable to search LDAP server");
            $entries = ldap_get_entries($ldapconn, $result); 
            $userDN = $entries[0]["ikealegacyuid"][0];          

            echo ('<p style="color:green;">I have the user DN: '.$userDN.'</p>');
            $url = 'Location: logpage01.html?uid=' .$userDN;
            echo $url;
            header($url);
        } else {
         //echo "<script type='text/javascript'>$('#myModal').modal('show');</script>";
                //echo "<script type='text/javascript'>$('#myModal').fadeIn('show');</script>";
                echo "<script>
         $(window).load(function(){
             $('#myModal').modal('show');
         });
    </script>";
        }

    }

}
?>
<html>

   <head>
      <title>Login Page</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }
         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }
         .box {
            border:#666666 solid 1px;
         }
      </style>

   </head>

   <body bgcolor = "#FFFFFF">

      <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>

            <div style = "margin:30px">

               <form action = "" method = "post">
                  <label>UserName  :</label><input type = "text" name = "username" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "password" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>



            </div>

         </div>

      </div>
      <div class="container">

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <!-- <button type="button" class="close" data-dismiss="modal">&times;</button> -->
          <h4 class="modal-title">System Message : Failure</h4>
        </div>
        <div class="modal-body">
          <p>Sorry !! Something went wrong. Please contact your administrator.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal" onclick="window.location.href = 'logpage01.html';">Close</button>
        </div>
      </div>

    </div>
  </div>

</div>

   </body>
</html>

我尝试了许多在这里找到的解决方案。但是没有任何效果。在这方面需要一些帮助。

更新:还选中了页面来源。好像还好如果没有,请让我知道这个问题。 enter image description here

我也尝试在页面加载时调用模式请求。运行良好。

<script type="text/javascript">
    $(window).on('load',function(){
        $('#myModal').modal('show');
    });
</script>

2 个答案:

答案 0 :(得分:0)

如果我正确看到它,则说明缺少引导程序中必需的popper.js。请检查此链接https://getbootstrap.com/docs/4.3/getting-started/introduction/,您将获得可使用的Popper在线CDN。或者直接复制我的代码段。另请注意,您正在版本3中使用引导程序。我强烈建议您使用V4。

在php中,当您想显示模态时,只需回显以下内容。

echo "<script>
             $('#myModal').modal('show');
       </script>";

   $('#myModal').modal('show');
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <!-- <button type="button" class="close" data-dismiss="modal">&times;</button> -->
          <h4 class="modal-title">System Message : Failure</h4>
        </div>
        <div class="modal-body">
          <p>Sorry !! Something went wrong. Please contact your administrator.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal" onclick="window.location.href = 'logpage01.html';">Close</button>
        </div>
      </div>

    </div>
  </div>

答案 1 :(得分:0)

仅在此处无法执行脚本,最好的情况是,您的浏览器最终将在加载加载jQuery的html之前运行引用$的脚本,因此即使运行您的脚本,它也会抛出{{ 1}}。

相反,您可以使用标志来告诉浏览器是否显示模式。例如,您可以在else(和catch)语句中设置一个变量:

Uncaught ReferenceError: $ is not defined

并将其插入html,以便浏览器知道在if ($ldapbind) { /*...*/ } else { $access_denied=1; } 时必须加载模态,为此,您还需要添加仅在该条件下才能加载模态的脚本:

access_denied===1

在另一个使用ajax请求的上下文中,您可能只是回显一些json或直接输出模式html。