dqpkea9486 2019-05-15 12:52
浏览 650
已采纳

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

I am working to validate users from LDAP via PHP. I have tried some code and I can now validate users perfectly. But in the case of an unsuccessful login, I want to display a modal popup with some custom message. Here is my code -

<?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>

I tried many solutions found here. But nothing worked. Need some help on this.

UPDATE: Checked Page Source Also. Seems it's fine. If not, please let me know the issue. enter image description here

I tried to call modal on page load also. It is working perfectly.

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

2条回答 默认 最新

  • doushang4274 2019-05-15 19:25
    关注

    Just echoing the script won't work here, at best your browser will end up running a script that references $ before loading the html that loads jQuery, so even if it runs your script it will throw Uncaught ReferenceError: $ is not defined.

    Instead you can use a flag to tell the browser whether or not to show the modal. For example, you can set a variable in your else (and catch) statement :

    if ($ldapbind) { /*...*/ }
    else {
      $access_denied=1;
    }
    

    and insert it into the html so that the browser knows it must load the modal when access_denied===1, for this you also need to add the script that will load the modal only under that condition :

    <div id="myModal" access_denied="<?php print $access_denied ?>" > ... </div>
    <script> 
      $('#myModal[access_denied=1]').modal('show');
    </script>
    

    In another context using ajax requests you would probably just echo some json or output the modal html directly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建