Mar 6, 2024 06:54 AM
(This post was last modified: Mar 6, 2024 08:57 AM by Hackers.)
In this very short guide you will learn how to create a POST request from a C++ program to your website, and how to store that data to a .txt file on a server with PHP.
For the client side we will use WinHTTP which can handle both http and https requests to websites. This is a much more native solution than something like curl which would require openssl or similar alongside it to do an https:// web request.
Our client program is less than 20 lines of code, and all it does is just send 2 string values (something, with value of 'goes', and here with value of 'booya'). We can modify that to send string versions of any data format, or other information we need to save to the server.
C++ Client
On the server side we make use of a tmp folder that isn't accessible to the public to store our data. This is a very basic script that will save a .txt file any time there is a new request. It is set up to save GET or POST requests, but our C++ client only sends POST. It also stores something called 'REMOTE_ADDR' which is the IP address of the requesting user (the device the C++ client is on).
PHP Server (your-website.com/post/receive.php)
This is a very primitive example but shows how a C++ client can send data to a PHP server to save for review later.
I hope this helps you develop out your latest ideas! :)
For the client side we will use WinHTTP which can handle both http and https requests to websites. This is a much more native solution than something like curl which would require openssl or similar alongside it to do an https:// web request.
Our client program is less than 20 lines of code, and all it does is just send 2 string values (something, with value of 'goes', and here with value of 'booya'). We can modify that to send string versions of any data format, or other information we need to save to the server.
C++ Client
Code
#include <windows.h>
#include <winhttp.h>
#include <iostream>
#pragma comment(lib, "winhttp.lib")
int main() {
HINTERNET hSession = WinHttpOpen(L"Send Post Request/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
HINTERNET hConnect = WinHttpConnect(hSession, L"your-website.com",INTERNET_DEFAULT_HTTPS_PORT, 0);
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/post/receive.php", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
std::wstring headers = L"Content-Type: application/x-www-form-urlencoded";
std::string postData = "something=goes&here=booya";
BOOL bResults = WinHttpSendRequest(hRequest, headers.c_str(), headers.length(), (LPVOID)postData.c_str(), postData.length(), postData.length(), 0);
if (bResults) bResults = WinHttpReceiveResponse(hRequest, NULL);
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
return 0;
}On the server side we make use of a tmp folder that isn't accessible to the public to store our data. This is a very basic script that will save a .txt file any time there is a new request. It is set up to save GET or POST requests, but our C++ client only sends POST. It also stores something called 'REMOTE_ADDR' which is the IP address of the requesting user (the device the C++ client is on).
PHP Server (your-website.com/post/receive.php)
PHP Code
<?php
$tempFolderPath = __DIR__ . '/tmp/';
$data = json_encode(["POST" => $_POST, "GET" => $_GET, "IP" => $_SERVER['REMOTE_ADDR']]);
$filePath = $tempFolderPath . '/receivedData-' . time() . '.txt';
$fileHandle = fopen($filePath, 'a');
if ($fileHandle === false) {
echo "Failed to open file for writing.";
exit;
}
fwrite($fileHandle, $data);
fclose($fileHandle);
echo "Data received and stored successfully.";
?>This is a very primitive example but shows how a C++ client can send data to a PHP server to save for review later.
I hope this helps you develop out your latest ideas! :)


