2011-11-15 12 views
1

私は、PHPスクリプトを通して検証されたユーザー名とパスワードの資格情報を持つログイン画面を持っています。ユーザーがユーザー名とパスワードを入力して送信を開始するとauthenticate.phpはLDAPバインドを行い、ユーザーの資格情報が合法であればwww.siteA.comとリダイレクトされ、資格情報が偽であれば "無効なログイン、認証に失敗しました"というerorrが与えられます。この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; 
    } 
?> 

答えて

2

すべてを貼り付ける必要はありません。

Ajaxを使用してフォームを送信し、サーバー側のスクリプトでステータスとエラーメッセージが返されるようにします。

ステータスがログインに成功した場合は、JavaScriptを使用してリダイレクトを行います。

ステータスがログインに失敗した場合は、エラーメッセージをアラートします。

+0

私はajaxを使用し、submitとログインにプロパティonclickを使用すれば、ログインが成功したら、PHPのリダイレクトが成功するでしょうか?それが失敗した場合、私は単にhtml divにエラーを出力しますか? – Bulvak

+0

私はあなたがそれを扱うためにいくつかのAjaxチュートリアルを行う必要があると思います。あなたの答えから、私はあなたがそれを前にやったことがないと推測しています。 Ajaxは(定義上)非同期であるため、ハンドルを取得するのは非常に難しいことです。いくつかのチュートリアルを行うと、それはあなたに明らかになります。 –

+0

iveはサーバーの時間を使ってテキストボックスの値を塗りつぶしたajaxチュートリアルを完了しました – Bulvak

関連する問題