AJAX login working sporadically in IE
Oh joy, another thing IE apparently does poorly.
I say "working sporadically" in the title because I've actually been able
to get the code to work in IE, but there have been times (without any code
changes) where it hasn't worked, and apparently the same is true for one
of my clients.
My interim solution was to tell him to "stop using IE," but in the
meantime, I feel it's my duty to investigate.
The problem: The login process seems to churn for a bit and then halt,
without throwing the error message that would typically accompany a bad
username or password. This is in IE only (my version is IE9, but I can't
guarantee this issue is confined to that version alone). The login works
perfectly in all other tested browsers, including mobile ones.
Javascript:
$('#login-box > form').submit(function(){
$('#login-response').html("<img src='assets/images/wait.gif'
height='32' />");
var str = $(this).serialize();
$.ajax({
type: 'POST',
url: 'lib/check-login.php',
data: str,
cache: false,
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(errorThrown + '\n' + textStatus);
},
success: function(msg) {
var output = msg.split('|');
var delay = 1600;
if (output[0] == 'OK') {
if (output[1] == '') { //no referrer, redirect to home page
window.location = home_page;
}
else { //referrer set, redirect back to referring page
console.log('saying referer');
//window.location = output[1].substring(1);
window.location = home_page;
}
}
else {
$('#login-response').html('no, denied');
$('#login-box input').val('');
$('#login-box input#username').focus();
}
}
});
PHP:
<?php
$sess = session_id();
if ($sess == '')
session_start();
require_once('config.php');
try_login($_POST['username'], $_POST['password']);
/****** user has submitted credentials, check them and return value to
javascript (AJAX) ***/
function try_login($u, $p) {
global $mysqli;
$res = $mysqli->query(' select u.user_id, u.user_name, u.user_pass,
s.salt, ifnull(u.team_id,0) team_id
from users u left join seasoning s on
u.user_id = s.user_id
where u.user_name = \'' .
$mysqli->real_escape_string($u) . '\'
limit 1;') or die($mysqli->error);
$row = $res->fetch_assoc();
if($row['user_pass'] == crypt($p,$row['salt'])) { //correct
password, log user in
if (isset($_POST['remember'])) //keep user logged in
$duration = time()+60*60*24*365;
else //end user session when he closes browser
$duration = false;
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['team_id'] = $row['team_id'];
$mysqli->query("update users
set
last_login = '" . date('Y-m-d H:i:s') . "',
login_qty = login_qty + 1
where user_id = " . $_SESSION['user_id'] . ";") or
die($mysqli->error);
$mysqli->query("insert into
activity ( activity_type_id,
user_id)
values (8,
" . $_SESSION['user_id'] . ");") or
die($mysqli->error);
echo 'OK|' . $_SESSION['last_page'];
}
else { //login failed
echo 'FAIL';
}
}
?>
No comments:
Post a Comment