2 Replies - 4562 Views - Last Post: 03 November 2011 - 10:07 AM

#1 Cbeppe  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 215
  • Joined: 16-September 09

[SOLVED] JQuery code working in Firebug, but not without...

Posted 02 November 2011 - 02:19 PM

Hey,

Not sure how else to describe this...
The following function works perfectly when I open the firebug script panel and define breakpoints at relevant lines, but when I don't, the code returns an entirely different result. It seems to be ignoring certain parts of the script when the breakpoints aren't defined, yet I wouldn't know exactly what part is ignored since I have to close the Firebug panel to replicate it. Yes... it's a paradox.

Here's the code, if there's some obvious mistakes that I just don't see, then I guess that may well be the problem.
function authenticate(){
	var cookiepass; /*** Valid password hash for the cookie (This uses a unique salt for each user, making it different from the 'truepass' variable.)***/
	var truepass; /* Valid password hash. The prompt() reply is compared to this hash.*/
	var replyhash; /* Correctly hashed version of the prompt() reply.*/
	/* Get contents of the 'access' cookie */
	var cookie = readCookie('access');
	/* Query PHP script to get the true cookiepass */
	$.post("database.php", { pass: cookie, type: 'cookie'},
		function(data) {
			cookiepass = data;
		}
	);

	if(cookie == cookiepass){
		/* If cookie contents matches the true cookiepass, return true. */
		return true;
	}else{
		/* Else, prompt the user for password. */
		var reply = prompt('Enter Password:', "");
		/* Query database for all three passes, type goes into a switch statement that should go to 'default'. */
		$.post("database.php", { pass: reply, type: ''},
			function(data) {
				/* The passes are separated by '[BRK]' statements: Cookiepass[BRK]Replyhash[BRK]Truepass */
				var passes = data.split("[BRK]");
				truepass = passes[2];
				replyhash = passes[1];
				cookiepass = passes[0]
			}
		);
		if(replyhash == truepass){
			/* If the hashed form of the reply matches the truepass, set a cookie with the valid cookiepass, then return true.*/
			setCookie('access',cookiepass,7);
			return true;
		}else{
			/* If not, alert the user, then return false.*/
			alert("Wrong password!")
			return false;
		}
	}
}



The script is supposed to check for a password, even though that's pretty much a useless thing to do client-side. The password is hashed server-side in the database.php file, but I don't have access to an SQL database or the .htaccess file. Therefore, I'm forced to find a PHP-JScript solution to it.

Mainly though, has anyone had the problem before, that a piece of code works when breakpoints are set in Firebug, but not without?

Thanks in advance for any help ;)

Cbeppe.

This post has been edited by Cbeppe: 03 November 2011 - 10:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED] JQuery code working in Firebug, but not without...

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: [SOLVED] JQuery code working in Firebug, but not without...

Posted 02 November 2011 - 02:55 PM

Hi,

If I had to guess I would say it's because your AJAX calls are asynchronous. That means the rest of the app runs while the call is being made. However if you have break points set then the script will pause execution and allow for requests to finish thus getting different results.

Looking at your code I can see places where that happens, you are assigning values to cookiepass in AJAX callbacks.

Using $.ajax() instead of $.post() should clear up the issue. Just make sure to set async to false as it defaults to true.

Hope this helps :).
Was This Post Helpful? 1
  • +
  • -

#3 Cbeppe  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 215
  • Joined: 16-September 09

Re: [SOLVED] JQuery code working in Firebug, but not without...

Posted 03 November 2011 - 10:07 AM

Thank You!

That was it. Works like a charm now :)

Cbeppe.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1