views:

199

answers:

1

The code below gets the username/password and runs it thru the backend.php script.

<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
        $(document).ready(function() {
            $("#login_form").submit(function() {
                var unameval = $("#username").val();
                var pwordval = $("#password").val();
                $.post("backend.php", { username: unameval, 
                password: pwordval }, function(data) {
                    $("#status p").html(data);
                });
                return false;
            });
        });
    </script>
    </head>
<body>
<?php
    if(isset($_SESSION['user_session'])) {
        print '<p>user '.$_SESSION['user_session'].' logged in</p>';
    } else { ?>
        <form id="login_form" method="post">
            <p>Username: <input type="text" id="username" name="username" /></p>
            <p>Password: <input type="password" id="password" name="password" /></p>
            <p><input type="submit" value="Login" /></p>
        </form>
        <div id="status">
            <p></p>
        </div> <?php
    }?>

</body>
</html>

It authenticates the user properly, but what I want it to is hide the form once successful.

Here is what the page looks like before the form is submitted: http://grab.by/3hoH

Here is the page after the form was successfully submitted: http://grab.by/3hoR

I think I need to run some javascript to check for success, then if success, do not display form, if !success, display form.

I am unsure on how to do that, if that is the case.

backend.php code

<?php
session_start();
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once('adLDAP.php');
//header('Content-type: text/json');
$adldap = new adLDAP();

if(isset($_POST['username']) && isset($_POST['password'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];  // associated password

    // connect to ldap server
    $authUser = $adldap->authenticate($username, $password);
    if ($authUser === true) {
        print "<p>authuser true</p>";
        $_SESSION['user_session'] = $username;

        if(isset($_SESSION['user_session'])) {
            print "<p>session is set</p>";
        }   
    }
    else {
      print "<p>User authentication unsuccessful</p>";
    }
}
?>
+1  A: 

You need to check the response to see if the login succeeded, and, if it did, remove the login form.

You can remove the login form by writing

 $('#login_form').remove();

To check whether the login succeeded, you should change your server-side script to return a JSON object that indicates whether the login succeeded and contains a message.
For example:

{ success: true, message: "You have successfully logged in" }
SLaks