PDA

View Full Version : AJAX Authentication



cdoverlaw
06-23-2009, 08:24 AM
I am trying to write a routine that checks my user data is valid and then returns true or false, this routine is used where I need a quick response to whether the user is a valid user or not.




function Authenticate(){

//Read Cookies
var Username = readCookie('Username');
var Password = readCookie('Password');
var AuthLevel = readCookie('AuthLevel');
var response = "FALSE"

var url="db/getuser.php";
var params="username=" + Username + "&password=" + Password

xmlhttp.onreadystatechange=function () {

if (xmlhttp.stateChange==4) {

if (xmlhttp.status==200) {


var sString = xmlhttp.responseText;
var sArray = new Array();

//Split the returned string
sArray = sString.split(",");

response = sArray[0];

}

}
}

xmlhttp.open("POST",url,true);

//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");

xmlhttp.send(params);

return response;
}


Unfortunately this always returns false

Thanks
Jonathan

ShadowIce
06-23-2009, 03:34 PM
now im not a an ajax genius, but i DID manage to see 1 error u made:

change:


function Authenticate(){

//Read Cookies
var Username = readCookie('Username');
var Password = readCookie('Password');
var AuthLevel = readCookie('AuthLevel');
var response = "FALSE"

var url="db/getuser.php";
var params="username=" + Username + "&password=" + Password

xmlhttp.onreadystatechange=function () {

if (xmlhttp.stateChange==4) {

if (xmlhttp.status==200) {


var sString = xmlhttp.responseText;
var sArray = new Array();

//Split the returned string
sArray = sString.split(",");

response = sArray[0];

}

}
}

xmlhttp.open("POST",url,true);

//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");

xmlhttp.send(params);

return response;
}

to:


function Authenticate(url){

//Read Cookies
var Username = readCookie('Username');
var Password = readCookie('Password');
var AuthLevel = readCookie('AuthLevel');
var response = "FALSE"

var url="db/getuser.php";
var params="username=" + Username + "&password=" + Password

xmlhttp.onreadystatechange=function () {

if (xmlhttp.stateChange==4) {

if (xmlhttp.status==200) {


var sString = xmlhttp.responseText;
var sArray = new Array();

//Split the returned string
sArray = sString.split(",");

response = sArray[0];

}

}
}

xmlhttp.open("POST",url,true);

//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");

xmlhttp.send(params);

return response;
}

call authenticate like this man:


<script>Authenticate('webpage.html');</script>

Hope that helps! :)

Good luck! :)

~SI~

cdoverlaw
06-23-2009, 04:16 PM
Why would I pass in a URL, the url is being filled in in the routine

ShadowIce
06-23-2009, 04:20 PM
Sorry, didnt see the var there.

cdoverlaw
06-23-2009, 04:40 PM
LOL ok, was bit confused by your change,