Jump to content

XMLHttpRequest problem


Porl123

Recommended Posts

I have two functions; one which first takes a list using XMLHttpRequest from a file called gatherList.php, splits it by \n, then using setTimeout every second adds a line to a DIV. Once this list has reached the end, it calls a second function that runs another file called endActions.php and I want this to only be run after the list has filled. The problem is that the user can just view the endActions.php page in their browser before the list has filled, so I need to pass over a key from the main file to the endActions.php page through a php GET. The problem is that I can't use a cookie, as the user could edit it and I can't use a random number using Math.random() because that can't be compared on the endActions.php page. I also can't save the value to either a file or a mysql table through another XMLHttpRequest as the user could go to saveKey.php?x= and enter their own code, then run the endActions.php page using it.

 

I also can't use php sessions as they would be visible in the source, allowing the user to copy them and access the endActions.php file with it.

It's a difficult situation and I can't really think of how else to explain it but if you know of any way I can make it so only the main page can access the endActions.php page, I'd appreciate it if you could help me out. I've posted this on the php board but it's probably more of an ajax issue. Anyway, thanks in advance!

Link to comment
Share on other sites

There are ways to hide the code from the average user from using the "Show Source", but anyone with firefox and a firebug plugin will get around it and those are the guys you are worried about, not the casual user.

 

Instead of hiding it, why not just validate the event calling the function?

Link to comment
Share on other sites

I also can't save the value to either a file or a mysql table through another XMLHttpRequest as the user could go to saveKey.php?x= and enter their own code, then run the endActions.php page using it.

 

Either validate the x variable or have it sent via the POST method.  One thing I have used is a db trigger.

 

Have a column called "showend" with a value of 0.  On displaying the last line, flip that to 1 and make your endActions.php file dependant on the value of showend.

Link to comment
Share on other sites

But he'd have to know what to send and it would not be visible in the browser url bar.  If that variable was a random number generated upon the 1st line display and stored in a db, then passed on the last line and verified against the one stored in the db, there would be no way of knowing what that variable was until after the final code was executed.

 

One thing that might help is if I knew exactly what this is supposed to be.  Only thing I can think of is a trivia game or something similar where is gives hints.

Link to comment
Share on other sites

No.  If I use php to generate and send the security code to mysql, and at the end use php mysql to pull that code to verify that the code sent via post was correct, you should have a secure system.  I had a similar problem in a game I run.  It had a series of challenges to win a big prize.  To avoid people attempting to run the final JS command out of sequence, I set up a step column in the user db.  It set it to 0 at the start of the challenge and after each sequence updated the database with a new step value.  At teach challenge, it checked the step column to verify that the value was valid.  If it wasn't, I had it send me a nice little message about who was doing it.  Got rid of a few hackers that way.

Link to comment
Share on other sites

Yeah sure.

 

				var battleLog,battleList,battleCount,i = 0;
				function runBattle() {
					var xmlHttp = connect();
					if(xmlHttp != false) {
						xmlHttp.open('GET', '/inc/battle/runBattle.php?' + Math.random(), true);
						xmlHttp.onreadystatechange = function() {
							if(xmlHttp.readyState == 4) {
								battleLog = xmlHttp.responseText;
								battleList = battleLog.split('\n');
								battleCount = battleList.length;
								addLine();
							}
						}
						xmlHttp.send('null');
					}
				}
				function addLine() {
					if((i + 1) == battleCount) {
						var xmlHttp = connect();
						if(xmlHttp != false) {
							xmlHttp.open('GET', '/inc/battle/endBattle.php?' + Math.random(), true);
							xmlHttp.onreadystatechange = function() {
								if(xmlHttp.readyState == 4) {
									document.getElementById('battleResponse-1').innerHTML = xmlHttp.responseText + document.getElementById('battleResponse-1').innerHTML;
								}
							}
							xmlHttp.send('null');
						}
					} else if((i + 1) < battleCount) {
						document.getElementById('battleResponse-1').innerHTML = battleList[i] + '<br />' + document.getElementById('battleResponse-1').innerHTML;
						document.getElementById('battleResponse-2').innerHTML = battleList[i];
						i++;
						setTimeout("addLine();",800);
					}
				}
				window.onload = runBattle;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.