2

I have setup an Apache webserver on my computer using the tool XAMPP. It is running a PHP script that checks for POST requests and stores POST values to a txt file.

<html>
<body>
<?php
$filename = "temperatures.txt";
$current_content = file_get_contents($filename);
if (!empty($_POST))
{
    $temperature = $_POST["temperature"];
    $new_contents = $current_content . $temperature . "\n";
    file_put_contents($filename, $new_contents);
}
else
{
    $new_contents = $current_content . "unknown\n";
    file_put_contents($filename, $new_contents); 
}
?>
</body>
</html>

I tested the script by generating a POST request using Curl, a command line tool. I did this from the local machine on which the server is running. This is the command that I ran:

curl -i -X POST -H 'Content-Type: application/json' -d "temperature=10" http://192.168.88.254/index.php"

Then I also have an IoT gateway/router within the same LAN network, which also sends HTTP POST requests. Using Wireshark I verified that packets from both sources are received by the machine.

The first entry is from the IoT gateway, the second one from curl.

  1. enter image description here

  2. enter image description here

However, the script outputs the 'unknown' value to the file when the IoT gateway was the source.

Does anyone have any idea why Wireshark catches the post value from the IoT gateway, but the server does not?

temperatures.txt
10
unknown

Results in apache log

192.168.88.254 - - [12/Mar/2021:11:40:06 +0100] "POST /index.php HTTP/1.1" 200 29 "-" "curl/7.55.1"
192.168.88.1 - - [12/Mar/2021:11:40:25 +0100] "POST /index.php HTTP/1.1" 200 29 "-" "Mikrotik/6.x Fetch"
Dirk
  • 95
  • 1
  • 8
  • As per the [How To Ask](https://stackoverflow.com/help/how-to-ask) guide, which you are encouraged to read before using the site, please don't post images of your code or data. Both of these things are text. Pasting it as graphics is very impractical as it can't be copied, searched, re-used in answers etc. It makes it difficult for those who might want to help you. Please edit your question to include the information as text and use the [formatting tools](https://stackoverflow.com/help/formatting) to present it nicely, so that it is usable for those who want to help you. Thanks – ADyson Mar 12 '21 at 09:46
  • Which of the wireshark entries is from the cURL request, and which is from the gateway? The second one seems to be forwarding the request to itself. – ADyson Mar 12 '21 at 09:50
  • P.S. Your sample results file doesn't contain either of the values from your wireshark samples. If you're going to show data, make it consistent end to end. – ADyson Mar 12 '21 at 09:52
  • I updated my question as you suggested. The first entry is from curl, second one from the IoT gateway. You are also right that the data was incosistent. I accidently mixed up the screenshots. Now it is correct – Dirk Mar 12 '21 at 09:59
  • Thanks. `The first entry is from curl, second one from wireshark` ...you mean the second entry is from the IoT device, I think? I updated your question to include this information, presuming that's the phrasing you intended. – ADyson Mar 12 '21 at 10:00
  • The odd thing I notice about the second request is that the source and destination are both 127.0.0.1 (i.e. the local loopback). It seems the gateway sends the request to itself?? Does it then get forwarded on later to the IP of the apache server? – ADyson Mar 12 '21 at 10:02
  • I am sory for all the confusing. The first one is actually from the IoT gateway, source (192.168.88.1) and the destination is my pc (192.168.88.254). The second requests is a loopback, just to test the script. With the loopback the post data is logged, with the gateway as source it is not – Dirk Mar 12 '21 at 10:05
  • Ok I see. That makes more sense. So finally the question is in a usable state. I have to say, just from that information I can't see why you'd have this problem. It all looks fine from what's shown - so I think we need more detail. Do the Apache logs have anything to say about it? I know they don't normally record POST values, but they do record what type of request came in. The only thing I can think of, which would be shown by the logs (or possibly by other bits we can't see in your wireshark screenshots) is that the gateway isn't sending a HTTP POST - maybe it sends a GET, or something else – ADyson Mar 12 '21 at 10:19
  • I just added the apache logs to the question. I do not see anything odd here. – Dirk Mar 12 '21 at 10:42
  • Thanks. I agree, there's nothing strange there. I think the next step might be to get Apache to dump out the POST data so you can see whether the data you can see in Wireshark is definitely being received correctly by the server - this is before it reaches PHP, so it's another link in the chain. We need to establish precisely where the data is failing to be received (or at least, failing to be understood as POST data). https://stackoverflow.com/questions/989967/best-way-to-log-post-data-in-apache might help you set this up. – ADyson Mar 12 '21 at 10:44

0 Answers0