emorjon2 0 Newbie Poster

Hi all!

I am developing a java-game where you are building with balls. It will connect to a server to create a huge map available for everybody playing this game.:)

For this, I need a PHP-file who will append some urldata into the file data.txt, for adding a ball into the server map.

But when I run this file for testing, something very stranged happend. I can't explain it, but I must fix it if i want to be done with my game.:S

the PHP file is called addball.php and the url goes like this:

http://localhost/ballania/addball.php?ball=11x10x1x0x0

the data I want to transport to see if it work is

11x10x1x0x0

But when the PHP file appended it to the data file, the file goes from empty into this

11

I tried to add some code the urldata goes from this in the URL

11o10o1o0o0

to this in the file:

11x10x1x0x0

but still, it would be this:

11

:yawn:

heres the code for it. I don't know what code does manipulate the data but I hope that
you guys would found it.

if (isset($_REQUEST["ball"])){
	 $fname = "data.txt";
	 echo $_REQUEST["ball"];
	 $f = fopen($fname, "a");
	 $filedata = str_replace("o", "x", $_REQUEST["ball"]);
	 echo $filedata;
         //here it starts to go something wrong
	 fwrite($f, str_replace("o", "x", $_REQUEST["ball"]) + "_");
	 fclose($f);
}

PLEEEEEEEEEEEAAAAAAAAAAAAASE can someone help me?????? :'( I will be SOOOOOOOOO happy
for answers!

PS. If there's any bad english in here, I'm sorry. I'm swedish

Dani AI

Generated

Short version: the file gets "11" because the code uses + (numeric addition) instead of string concatenation. In PHP + forces numeric conversion of strings (so "11x10x1x0x0" becomes 11), which is why your echoed string looks right but the written value is just 11. See the PHP notes on type juggling for details: PHP type juggling.

A safe fix is to concatenate with . and append a newline, and use file_put_contents() with FILE_APPEND|LOCK_EX so writes are atomic. Example:

if (isset($_REQUEST['ball'])) {
    $ball = str_replace('o', 'x', $_REQUEST['ball']);
    file_put_contents('data.txt', $ball . PHP_EOL, FILE_APPEND | LOCK_EX);
    echo $ball;
}

See PHP string operator docs for .: string operators, and file_put_contents() usage: file_put_contents.

Other tips and checks:

  • If the data might include URL-special characters (spaces, +, &), URL-encode it on the client side and decode on the server; + in a URL will be interpreted as space by application/x-www-form-urlencoded. See rawurlencode.
  • Prefer POST for operations that modify server state and validate/sanitize input before writing.
  • Ensure data.txt has correct write permissions and use locking (the LOCK_EX flag above or flock) to avoid race conditions.
  • For debugging, var_dump() the expression you write to disk to verify its runtime value before writing.

For : swapping + for . in the write expression will fix the immediate issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.