1
我有一個用戶名和密碼憑證通過php腳本驗證的登錄屏幕。一旦用戶輸入他們的用戶名和密碼並點擊提交。 authenticate.php執行LDAP綁定,如果用戶的憑證是合法的,他們將被重定向到www.siteA.com,如果憑證是false,則會給出錯誤提示「無效登錄,身份驗證失敗。我想讓這個AJAX如此當用戶點擊提交,正確的網頁上,它告訴用戶他們是否進入有效或無效的登錄我如何才能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重定向,如果我使用ajax和使用屬性onclick提交和登錄成功將工作?如果它失敗了,那麼我只是打印到html div的錯誤? – Bulvak
我認爲你需要做一些Ajax教程來掌握它。從你的回答中,我猜你以前從未做過。 Ajax(按定義)是異步的,這可能是一個非常棘手的事情來處理。做幾個教程,它會變得很明顯。 –
我只做了一個ajax教程,它用服務器的時間填充文本框的值 – Bulvak