【问题标题】:how can I AJAX form authentication?如何进行 AJAX 表单身份验证?
【发布时间】:2011-11-15 23:08:44
【问题描述】:

我有一个登录屏幕,其中包含通过 php 脚本验证的用户名和密码凭据。一旦用户输入他们的用户名和密码并点击提交。 authenticate.php 进行 LDAP 绑定,如果用户的凭据是合法的,他们将被重定向到 www.siteA.com,如果凭据是错误的,则会给出错误提示“登录无效,身份验证失败。我想做这个 AJAX当用户点击提交时,就在该页面上,它会告诉用户他们输入的登录名是有效还是无效。

下面是我的代码:

登录表单:

<form action=authenticate.php method=post name=Auth class="appnitro">
    <div class="form_description">
    <h2>Login</h2>
    </div>
<ul>
        <li id="li_1">
    <label class="description" for="element_1">Username </label>
        <div>
        input id="element_1" name="login" class="element text medium" type="text" maxlength="255" value=""/>
        </div>
    </li>
        <li id="li_2" >
        <label class="description" for="element_2">Password </label>
        <div>
        <input id="element_2" name="password" class="element text medium" type="password" maxlength="255" value=""/>
        </div>
    </li>
        <li class="buttons">
            <input id="saveForm" class="button_text" type="submit" name="submit" value="Log In" />
    </li>
    </ul>
</form>

我的 LDAP 绑定,Authenticate.php:

<?php
    session_start();

    if( isset($_POST['login']) && isset($_POST['password']) )
    {
        //LDAP stuff here.
        $username = trim($_POST['login']);
        $password = trim($_POST['password']);

        echo("Authenticating...");
        $ds = ldap_connect('ldap://ldap:port');

        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);

        //Can't connect to LDAP.
        if( !ds )
        {
            echo "Error in contacting the LDAP server -- contact ";
            echo "technical services!  (Debug 1)";

            exit;
        }

        //Connection made -- bind anonymously and get dn for username.
        $bind = @ldap_bind($ds);

        //Check to make sure we're bound.
        if( !bind )
        {
            echo "Anonymous bind to LDAP FAILED.  Contact Tech Services! (Debug 2)";

            exit;
        }

        $search = ldap_search($ds, "ou=People,DC=keler,DC=medioa,DC=com", "uid=$username");

        //Make sure only ONE result was returned -- if not, they might've thrown a * into the username.  Bad user!
        if( ldap_count_entries($ds,$search) != 1 )
        {
            echo "Error processing username -- please try to login again. (Debug 3)";
            redirect(_WEBROOT_ . "/try1b.php");

            exit;
        }

        $info = ldap_get_entries($ds, $search);

        //Now, try to rebind with their full dn and password.
        $bind = @ldap_bind($ds, $info[0][dn], $password);
        if( !$bind || !isset($bind))
        {
            echo "Login failed -- please try again. (Debug 4)";
            redirect(_WEBROOT_ . "/try1b.php");

            exit;
        }

        //Now verify the previous search using their credentials.
        $search = ldap_search($ds, "ou=People,DC=keler,DC=medioa,DC=com", "uid=$username");
        //if the user's login is legit then redirect to the siteA    
        $info = ldap_get_entries($ds, $search);
        if( $username == $info[0][uid][0] )
        {
            $_SESSION['username'] = $username;
            $_SESSION['fullname'] = $info[0][cn][0]; 
            header( "Location: www.siteA.com" );

            exit;
        }
        else
        {
            echo "Error. Access Denied";
            redirect(_WEBROOT_ . "/try1b.php");

            exit;
        }
        ldap_close($ds);
        exit;
    }
?>

【问题讨论】:

    标签: php javascript html ajax openldap


    【解决方案1】:

    不需要全部粘贴。

    使用 Ajax 提交表单,并让服务器端脚本返回状态和错误消息。

    如果状态表明登录成功,请使用 JavaScript 进行重定向。

    如果状态显示登录失败,提示错误信息。

    【讨论】:

    • 现在如果成功,登录,php 重定向,如果我使用 ajax 并使用属性 onclick 进行提交和登录成功,那会起作用吗?如果它失败了,那么我只是打印到 html div 错误?
    • 我认为您需要学习一些 Ajax 教程才能掌握它。根据您的回答,我猜您以前从未这样做过。 Ajax(根据定义)是异步的,处理起来可能是一件非常棘手的事情。做几个教程,你就会明白了。
    • 我只做了一个 ajax 教程,它用服务器的时间填充文本框的值
    • 做一些专门针对 Ajax 的 jQuery 教程;尤其是当它们以任何方式进行交互时,例如创建聊天服务器。
    • 我创建了一个 ajax 聊天室,所有的日志都用 html 编写并被读取,我只是坚持通过 ajax 提交它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 2015-06-26
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多