Jump to content
cnalo

save HTTP Post headers to MySQL

Recommended Posts

Hello, I am a newbie regarding PHP and have done mostly only front-end web design for a couple of years and recently started querying DB's and displaying the data, but now this is a new project.

1) I have an external application sending HTTP POST requests to a specific php page on my web server.
2) I would like to store each new POST in a MySQL table and sort the value of all header names in their designated columns and add the current timestamp as well.
The headers included are "X", "Z", "Player", "UUID"


my DB has the columns 'X', 'Z', 'Player', UUID', 'Timestamp'.

I've tested code snippets as the ones below, but fail to incorporate them in the bigger picture of what I'm trying to achieve.
This one saves the data into a CSV, but it rewrites the file, appending a new line would already be a big step forward.

<?php
// https://gist.github.com/magnetikonline/650e30e485c0f91f2f40

class DumpHTTPRequestToFile {

	public function execute($targetFile) {

		$data = sprintf(
			"%s %s %s\n\nHTTP headers:\n",
			$_SERVER['REQUEST_METHOD'],
			$_SERVER['REQUEST_URI'],
			$_SERVER['SERVER_PROTOCOL']
		);

		foreach ($this->getHeaderList() as $name => $value) {
			$data .= $name . ': ' . $value . "\n";
		}

		$data .= "\nRequest body:\n";

		file_put_contents(
			$targetFile,
			$data . file_get_contents('php://input') . "\n"
		);

		echo("Done!\n\n");
	}

	private function getHeaderList() {

		$headerList = [];
		foreach ($_SERVER as $name => $value) {
			if (preg_match('/^HTTP_/',$name)) {
				// convert HTTP_HEADER_NAME to Header-Name
				$name = strtr(substr($name,5),'_',' ');
				$name = ucwords(strtolower($name));
				$name = strtr($name,' ','-');

				// add to list
				$headerList[$name] = $value;
			}
		}

		return $headerList;
	}
}


(new DumpHTTPRequestToFile)->execute('./dumprequest.csv');

This snippet also seems to return the headers and their values but I don't know how to efficiently write and append it to a TXT or CSV, or preferably, directly into a MySQL database.

<?php
foreach (getallheaders() as $name => $value) {

    echo "$name: $value";
	echo "<br>";
}
?>

Any help towards storing the header values of HTTP POST requests into a database would be greatly appreciated!
Also, storing the body data in a separate column is optional, but nice to have.

Share this post


Link to post
Share on other sites

I sure hope the headers aren't actually "X", "Y", "Player", and "UUID". Are you saying that $_SERVER has entries like "HTTP_X" and "HTTP_PLAYER"? Because if so then whoever set this up needs to be re-educated about HTTP.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.