Results 1 to 5 of 5

Thread: AJAX Authentication

  1. #1
    Join Date
    May 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default AJAX Authentication

    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.

    Code:
    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

  2. #2
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    now im not a an ajax genius, but i DID manage to see 1 error u made:

    change:

    Code:
    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:

    Code:
    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:

    Code:
    <script>Authenticate('webpage.html');</script>
    Hope that helps!

    Good luck!

    ~SI~

  3. #3
    Join Date
    May 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Why would I pass in a URL, the url is being filled in in the routine

  4. #4
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Sorry, didnt see the var there.

  5. #5
    Join Date
    May 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    LOL ok, was bit confused by your change,

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •