tags:

views:

229

answers:

3

Hey,

I'm creating a browser based game and at the moment I'm designing the script that updates the position of my character when he/she clicks on a different part of the map.

I am using a bit of ajax to do this which send a request to a php file called position_update.php. But for it to work I need to send two values along with it so it knows what the new position is, how do I do this? Below is my link that send the request to php file.

<a onClick="positionUpdate();"><img src="images/transparent.gif" border="0" /></a>

As a note, I'm using the php $_GET super global to retrieve the values when they're sent.

Thanks, Stanni

+5  A: 

You need to hit the URL:

position_update.php?var1=val1&var2=val2

PHP will parse the get string into $_GET

singpolyma
+1  A: 

BTW, you should ensure that degrades gracefully by having an equivalent href value that works for non-js users.

Bobby Jack
IIRC, his application uses AJAX to render/update a tile-based game. So without javascript enabled, the user would not be able to play the game anyway.
Calvin
A: 

In case you need to encode the URL string, use the javascript function encodeURL():

sUrl = encodeURL("position_update.php?dir="+sDir+"&dist="+sDist);

And remember that in a client-server architecture, do not put too much implicit trust in the client. All of the application logic that handles game mechanics and rules enforcement should be contained in the server-side component.

A few years ago there was a really popular game called TetriNET (multiplayer online Tetris) that was designed without these security considerations. It took me about 3 days to crack the communication protocol and basically draw on other players' screens using server commands for creating/clearing blocks. It made cheating very easy using just a simple proxy.

So don't let the client 'tell' the server where the player is. Use the client only to display the interface, collect input, and display output. So have the client tell the server where the player 'wants' to move (direction, how far, etc.), and then have the server calculate the player's position based on what moves the player is legally allowed to make and his previous position.

Calvin