I want to make a simple website, with a login, and if the login is a certain string (eg Andrew) I want it to ask for a password with a JS prompt. After the password is typed, an ajax function checks a simple php file for the password.
To understand things better, here is the code:
function login()
{
if (document.getElementById("username").value == "" || document.getElementById("username").value == null)
{
alert("Wrong username... actually empty username :)\nTry again.");
return;
}
if (document.getElementById("username").value == "andrew")
{
var pass = prompt("Password : ", "");
if (pass == "")
{
alert("Your password is empty!\nYou fucked it or you're not really Andrew!");
return;
}
//THIS IF IS ALWAYS GOING IN ELSE
if (check_pass(pass))
{
alert("good password");
return;
}
else
{
alert("wrong");
return;
}
}
setCookie("username", document.getElementById("username").value, 365);
window.location.href = "chat.php";
}
This is the function that is called after I click the login button.
Here is the check_pass function that I'm using to check if it's the right password (this function works, but it's for reference):
function check_pass(password)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var checker = xmlhttp.responseText;
//alert(checker);
if (checker.indexOf('1') !== -1)
{
//alert("in true");
return true;
}
else
{
//alert("in false");
return false;
}
}
}
xmlhttp.open("POST", "checkpass.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("password=" + password);
}
In the check_pass function, I've put some alerts to test if I actually get the right values, and yes, if the response is 1 it should return true, if it's 0, it should return false. And it does that. But ... in the login function, this part of code:
if (check_pass(pass))
{
alert("good password");
return;
}
else
{
alert("wrong");
return;
}
Always returns false.
I am doing this since 1 hour now, and I can't see what's wrong. Any ideas ? I've also tried with check_pass(pass) === true and check_pass(pass) == true but it's always returning false.
Any suggestions would be appreciated.