0

Js code

var server = '';
var orig_chat = chatUpdateSucess;
chatUpdateSucess = function(o){
if (o.GlobalChats && o.GlobalChats.length > 0) {
    //TODO: Add setting to enable/diosable this
    console.log(JSON.stringify(o.GlobalChats));

    var xhr = new XMLHttpRequest();
    xhr.open("POST", server+"/api.php?request=log_gc");
    xhr.send(JSON.stringify(o.GlobalChats));

}
orig_chat.apply(this, arguments);
};

Server code named api.php

<?php 
header("Access-Control-Allow-Origin: *");
if(!empty($_POST['o.GlobalChats'])){
 $data = $_POST['o.GlobalChats'];
 $fname = time() . ".txt";//generates random name

  $file = fopen("" .$fname, 'w');//creates new file
   fwrite($file, $fclose($file);
  }

 ?>

console.log output [{"PlayerId":237186,"toPlayerId":0,"chatid":16606292,"added":"/Date(1451764948837)/","addedText":"20:02","PlayerLink":"p=Kodabear|237186|T?|78|1|0|0-144-0-240-186-0-0-0-0-0-0-0-0|#IKnowAFighter|Neurofibromatosis Awareness day/Month|5-404-282-59","text":"Exmaple of a real chat"}]

I created a js that sends a file to my server every time the chat in the game is updated. But I am having problems with the server side code any advice would be great help. (PHP code was founded here

Saving a text file on server using JavaScript

Community
  • 1
  • 1
kodabear
  • 340
  • 1
  • 14

1 Answers1

3

Try to var_dump($_POST['o.GlobalChats']) to see if your data is reaching the server.

It seems like you are not writing the file to the system properly. Please read the examples at the manual (http://php.net/manual/pt_BR/function.fwrite.php)

Also, using time() is not safe, because two files may be created at the same UNIX timestamps in extreme cases, and one will overwrite the other

Try something like this:

$data = $_POST['o.GlobalChats'];

$fname = time() . "-" . rand ( 1 , 10000 ) . ".txt";

$handle = fopen($fname, 'w');

fwrite($handle, $data);

fclose($handle);
caulitomaz
  • 2,141
  • 14
  • 20
  • 1
    well it is creating the new files but they are empty. – kodabear Jan 02 '16 at 21:36
  • 1
    [02-Jan-2016 17:34:20 America/Detroit] PHP Warning: fwrite() expects parameter 1 to be resource, null given in /home/kodabear/public_html/test/api.php on line 9 – kodabear Jan 02 '16 at 22:43
  • 1
    I added var_dump($_POST['o.GlobalChats']) to see if the server is getting the data and it is saying null and when i add var_dump(json_decode(file_get_contents("php://input"))); i get array(1) { [0]=> object(stdClass)#1 (7) { ["PlayerId"]=> int(237186) ["toPlayerId"]=> int(0) ["chatid"]=> int(16869047) ["added"]=> string(21) "/Date(1453246164937)/" ["addedText"]=> string(5) "23:29" ["PlayerLink"]=> string(125) "p=Kodabear|237186|T?|78|1|0|0-144-0-240-186-0-0-0-0-0-0-0-0|#IKnowAFighter|Neurofibromatosis Awareness day/Month|5-404-282-59" ["text"]=> string(1) "5" } } – kodabear Jan 19 '16 at 23:29
  • See http://stackoverflow.com/questions/8893574/php-php-input-vs-post . You either are not sending anything as `o.GlobalChats` or are not sending your data through a `POST` request. – caulitomaz Jan 19 '16 at 23:45