0

PHP code is:

<?php

// Inialize session
session_start();

// Include database connection settings
include('config.inc');

// Retrieve username and password from database according to user's input
$sql ="SELECT * FROM user WHERE (email = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "')";

$login = mysql_query($sql);


// Check username and password match
if (mysql_num_rows($login) == 1) {
    // Set username session variable
    $_SESSION['username'] = $_POST['username'];
    // Jump to secured page
    header('Location: http://localhost/site/client/index.php');
}
else {
    // Jump to login page
    header('Location: http://localhost/site/acces-client/index.php');
}

?>

This works:

The following form can successfully get the user logged and redirected:

<table border="0">
<form method="POST" action="loginproc.php">
<tr><td>Email:</td><td>:</td><td><input type="text" name="username" size="30"></td></tr>
<tr><td>Mot de passe:</td><td>:</td><td><input type="password" name="password" size="30"></td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" value="Envoyer"></td></tr>
</form>
</table>

This does not work: by "does not work", I mean there is no redirect. Although the correct URL is called (http://localhost/site/client/index.php).

$.post("http://localhost/site/loginproc.php", {username: email, password: motdepasse})
    .success(function(result3){
    });
Timothée HENRY
  • 14,294
  • 21
  • 96
  • 136

2 Answers2

1

For the redirect to work, you'll need something like:

$.post("http://localhost/site/loginproc.php", {username: email, password: motdepasse})
    .success(function(result3){
        // won't reach here, since the php script never returns a 200 OK
    })
    .error(function(jqXHR, textStatus, errorThrown) {
        if(jqXHR.status == 302 || jqXHR.status == 301) {
            // 301 or 302 means redirected (perm, temp respectively)
            window.location.href = jqXHR.getResponseHeader('Location');
        }
    });
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • Thanks. I tried that code, it does not get to the .error part: I added a javascript alert in there which does not pop up. Actually it gets into the .success part (an alert there pops up), although the http status code is 'Status Code:302 Found (from cache)' – Timothée HENRY Jul 10 '12 at 12:31
  • Oh.. it is not supposed to go to `success()` if the status code is not 200 afaik! This is strange.. – techfoobar Jul 10 '12 at 12:32
  • perhaps this explains: http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call Am looking into this. – Timothée HENRY Jul 10 '12 at 12:46
  • Ok, it worked with: 'window.location = 'http://localhost/site/client/index.php';' – Timothée HENRY Jul 10 '12 at 12:48
0

Following works:

$.post("http://localhost/site/acces-client/loginproc.php", {username: email, password: motdepasse})
        .success(function(jqXHR, textStatus, errorThrown) {
        window.location = 'http://localhost/site/client/index.php';
    })
});
Timothée HENRY
  • 14,294
  • 21
  • 96
  • 136