0

Here is the code, I always have the $data variable set to "" as the requests aren't received properly:

<?php
echo"<h1>J_server</h1>";
//receive requests
$data = htmlspecialchars($_POST["incoming_data"] ?? "");
//if not received properly it's defined as ""
if($data != ""){
    if(file_exists("data.log")){
        $file = fopen("data.log","a");
    }else{
        $file = fopen("data.log","w");
    }
    fwrite($file,$data."\n");
    fclose($file);
}else{
    if(file_exists("data.log")){
        $file = fopen("data.log","a");
    }else{
        $file = fopen("data.log","w");
    }
    fwrite($file,"Error in recieving data!!!\n");
    fclose($file);
}
?>

here is the request code:

Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.post("http://localhost/j_server/server.php",data={"incoming_data":"Hello world!"})
<Response [200]>

1 Answers1

0

This seems to work for me:

<?php

    echo "<h1>Server</h1>";

    if ( isset ( $_POST ["incoming_data"] ) ) {
        $sanitized = htmlspecialchars ( $_POST ["incoming_data"] );
        file_put_contents ( "data.log", $sanitized . "\n", FILE_APPEND );
    }

?>
Raffi
  • 1,810
  • 2
  • 16
  • 25
  • It's still not working, I think it is something to do with receiving the data rather than processing it but I don't know what it is – John Smith May 26 '18 at 09:10
  • I don't think the code executes because the post request isn't set – John Smith May 26 '18 at 09:11
  • I just tested using python and requests the same way that you do and it worked for me. This could be a permission issue. https://stackoverflow.com/a/49566838/885475 – Raffi May 26 '18 at 13:10